我已经创建了一个flutter应用程序,它有一个带有按钮的文本字段,我希望我插入到文本字段的每个单词,当我按下按钮时,这些数据将被写入google工作表
我不明白为什么不管用
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import 'model.dart';
String tax = 'ert';
final wordi = TextEditingController();
String taxi = wordi.text;
class BillSplit extends StatefulWidget {
// const BillSplit({Key? key}) : super(key: key);
@override
State<BillSplit> createState() => _BillSplitState();
}
class _BillSplitState extends State<BillSplit> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
margin: EdgeInsets.only(left: 20, right: 20),
child: Column(children: [
Container(
alignment: Alignment.centerLeft,
margin: EdgeInsets.only(top: 30),
child: Text(
'split',
style:
GoogleFonts.abel(fontSize: 20, fontWeight: FontWeight.w600),
),
),
SizedBox(height: 15),
Container(
width: MediaQuery.of(context).size.width,
height: 300,
decoration: BoxDecoration(color: Colors.amber),
child: Column(
children: [
Text(
'total',
style: GoogleFonts.abel(
fontSize: 20, fontWeight: FontWeight.w600),
),
Text(
'${tax.toString()}',
style: GoogleFonts.abel(
fontSize: 20, fontWeight: FontWeight.w600),
),
Container(
width: MediaQuery.of(context).size.width / 2,
child: TextField(
keyboardType: TextInputType.number,
controller: wordi,
onChanged: (value) {
setState(() {
tax = value;
});
},
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
),
labelText: "Tax",
labelStyle: GoogleFonts.abel(
fontSize: 20, fontWeight: FontWeight.w300),
),
),
),
RaisedButton(
onPressed: () async {
// final body = json.encode(tax);
final url = Uri.parse(
'https://script.google.com/macros/s/AKfycbyt3ECPqAweBMQFgi96BqTkTzE_HPpFr0hwpGf9pisQ1asTp-g2C5geYfy2XAZUvd2-/exec');
final response = await http.post(
url,
// headers: {'Content-Type': 'application/json'},
body: json.encode({
taxi,
}),
);
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
}, // },
child: Text('Open'),
),
],
),
)
]),
),
);
}
}
// void sendData() {
// String taxi = wordi.text;
// }
我收到以下错误:
错误:将对象转换为可编码对象失败:在转换时,在Object中的HashSet实例[作为抛出](localhost:58716/dart_sdk.js: 5080:11)。_JsonStringStringifier.new.write eObject-
这里是google app脚本代码,应该从flutter app获取数据,需要将其写入google工作表
function doPost(e) {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1z6y1I3b8xm9XLg-W-VhMOn4d8uHSa0Kv6mK_kwVZu7M/edit#gid=0");
var sh = ss.getSheetByName("Sheet1");
sheetApp(e);
Logger.log(e.parameter);
}
function sheetApp(e) {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(4,1).setValue(e);
}
这是错误
Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 963:28 get current
at Object.createErrorWithStack (http://localhost:65241/dart_sdk.js:5093:12)
at Error._throw (http://localhost:65241/dart_sdk.js:20399:18)
at Error.throwWithStackTrace (http://localhost:65241/dart_sdk.js:20396:18)
at async._AsyncCallbackEntry.new.callback (http://localhost:65241/dart_sdk.js:40921:18)
at Object._microtaskLoop (http://localhost:65241/dart_sdk.js:40778:13)
at _startMicrotaskLoop (http://localhost:65241/dart_sdk.js:40784:13)
at http://localhost:65241/dart_sdk.js:36261:9
试试这个:
只是猜测而已
function sheetApp(e) {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(4,1).setValue(JSON.stringify(e));
}
关于你的评论。那么下面的记录器语句传递了什么?
function doPost(e) {
Logger.log(JSON.stringify(e));//Please provide this
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1z6y1I3b8xm9XLg-W-VhMOn4d8uHSa0Kv6mK_kwVZu7M/edit#gid=0");
var sh = ss.getSheetByName("Sheet1");
sheetApp(e);
Logger.log(e.parameter);
}
{出租车,}
不是有效的json,所以json. encode
会抛出。
你做不到
json.encode({
axi,
}),
json. encode
的参数需要是一个映射、一个列表或一个字符串。{axi}
是一个集合,不是一个有效的参数。也许可以尝试:
json.encode([
axi
]),
或
json.encode(axi),