android - 错误:没有为类“对象?”定义方法“数据”。- “对象”来自“飞镖:核心”
问题描述
错误:
警告:null-aware 操作的操作数 '??' 具有不包括空值的“字符串”类型。"$${document["price"]}" ?? “价格”,^ lib/screens/product_page.dart:38:65:错误:方法“数据”没有为“对象?”类定义。
- “对象”来自“飞镖:核心”。尝试将名称更正为现有方法的名称,或定义名为“数据”的方法。Map<String, dynamic> documentData = snapshot.data.data(); ^^^^
FAILURE:构建失败并出现异常。
其中:脚本 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' 行:1035
出了什么问题:任务“:app:compileFlutterBuildDebug”执行失败。
Process 'command 'C:\src\flutter\bin\flutter.bat'' 以非零退出值 1 结束
尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。运行 --scan 以获得完整的见解。
在https://help.gradle.org获得更多帮助
BUILD FAILED in 36s Exception: Gradle task assembleDebug failed with exit code 1
代码:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:your_store/constants.dart';
import 'package:your_store/widgets/custom_action_bar.dart';
import 'package:your_store/widgets/image_swipe.dart';
class ProductPage extends StatefulWidget {
final String? productId;
ProductPage({this.productId});
@override
_ProductPageState createState() => _ProductPageState();
}
class _ProductPageState extends State<ProductPage> {
final CollectionReference _productRef =
FirebaseFirestore.instance.collection("Products");
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
FutureBuilder(
future: _productRef.doc(widget.productId).get(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Scaffold(
body: Center(
child: Text("Error: ${snapshot.error}"),
),
);
}
if (snapshot.connectionState == ConnectionState.done) {
//Firebase document data map
Map<String, dynamic> documentData = snapshot.data.data();
//List of images
List imageList = documentData["images"];
return ListView(
padding: EdgeInsets.all(0),
children: [
ImageSwipe(imageList: imageList,),
Padding(
padding: const EdgeInsets.only(
top: 24.0,
left: 24.0,
right: 24.0,
bottom: 4.0,
),
child: Text(
"\$${documentData["name"]}",
style: Constants.boldHeading,
),
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 4.0,
horizontal: 24.0,
),
child: Text(
"\$${documentData["price"]}",
style: TextStyle(
fontSize: 18.0,
color: Theme.of(context).accentColor,
fontWeight: FontWeight.w600,
),
),
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0,
horizontal: 24.0,
),
child: Text(
"\$${documentData["desc"]}",
style: TextStyle(fontSize: 16.0),
),
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 24.0,
horizontal: 24.0,
),
child: Text(
"Selected Size",
style: Constants.regularDarkText,
),
),
],
);
}
//Loading State
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
},
),
CustomActionBar(
hasBackArrow: true,
hasTitle: false,
hasBackground: false,
),
],
));
}
}
解决方案
我认为您的第二个比较条件不完整,而不是第二个,如果请使用 if-else,此条件。
else if(snapshot.connectionState == ConnectionState.done && snapshot.hasData){...}
推荐阅读
- javascript - 扩展运算符在 Alexa 中抛出意外的令牌错误
- sql - Oracle Query如何计算具有值的列范围
- python - 从三个x,y点计算角度的更多pythonic方法返回0-360度,“向上”为0?
- javascript - 如何使用 javascript 选择 section 标签内的所有 div?
- google-apps-script - 添加脚本后撤消功能不起作用
- go - 将状态存储在 golang 二进制文件中
- excel - 如何根据特定标准将特定值从一张纸传递到另一张纸
- java - 如何从命令行创建具有所有模块依赖项的 javaFX 应用程序的 jar 文件?
- javascript - Material Web 组件与 Material 组件 Web
- python - 根据 Pandas 中的多个条件过滤行