【问题标题】:Showing Error (The operator '+' isn't defined for the type 'Uri'. Try defining the operator '+'.dart (undefined_operator))显示错误(未为“Uri”类型定义运算符“+”。尝试定义运算符“+”.dart (undefined_operator))
【发布时间】:2021-06-02 17:54:31
【问题描述】:

这是我的代码 这是用于将数据存储到谷歌电子表格的应用程序。
// 在 plus OPERATOR '+' 下显示红线 在 LINE 中

import 'dart:convert' as convert;
import 'package:store_data/models/update.dart';
import 'package:http/http.dart' as http;


class FormController {
  // Callback function to give response of status of current request.
  final void Function(String) callback;

  var url = Uri.parse('https://script.google.com/macros/s/AKfycbzFd5Cw0itASDzFgIrdr97j8PbfWHw1iVcoJWhbChPKfO2c2sA/exec');
  static const STATUS_SUCCESS = "SUCCESS";

  FormController(this.callback);

  void submitForm(Update update) async{
    try{
      await http.get(
        url + update.toParams()).then(
          (response){
            callback(convert.jsonDecode(response.body)['status']);
          });
    } catch(e){
      print(e);
    }
  }
}'

【问题讨论】:

  • 如果要附加到 URL,您需要先将 Uri 对象转换为字符串,附加,然后再将结果转换回来。或者更好的是,在您完成构建 URL 之前不要调用 Uri.parse
  • 您将Uri 对象视为字符串,但它不是字符串,并且Uri 类不支持+ 运算符。

标签: flutter dart operator-keyword


【解决方案1】:

我不确定。试试

var url ='...';

然后

void submitForm(Update update) async{
url+=update.toParams();
Uri uri = Uri.paste(url);
try{
  await http.get(
    uri).then(
      (response){
        callback(convert.jsonDecode(response.body)['status']);
      });
} catch(e){
  print(e);
}

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    相关资源
    最近更新 更多