我用扑扑上的火力打了一个简单的婴儿名字项目, 成功完成本教程后,应用程序将显示以下错误:“ package:firebase_demo / main.dart':失败的断言:第86行pos 16:'map ['votes]!= null':不正确。”
同样的帮助。
我的代码-:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
final dummySnapshot = [
{"name": "Filip", "votes": 15},
{"name": "Abraham", "votes": 14},
{"name": "Richard", "votes": 11},
{"name": "Ike", "votes": 10},
{"name": "Justin", "votes": 1},
];
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Baby Names',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() {
return _MyHomePageState();
}
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Baby Name Votes')),
body: _buildBody(context),
);
}
Widget _buildBody(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('baby').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildList(context, snapshot.data.documents);
},
);
}
Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
return ListView(
padding: const EdgeInsets.only(top: 20.0),
children: snapshot.map((data) => _buildListItem(context, data)).toList(),
);
}
Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
final record = Record.fromSnapshot(data);
return Padding(
key: ValueKey(record.name),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: ListTile(
title: Text(record.name),
trailing: Text(record.votes.toString()),
onTap: () => print(record),
),
),
);
}
}
class Record {
final String name;
final int votes;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['name'] != null),
assert(map['votes'] != null),
name = map['name'],
votes = map['votes'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$name:$votes>";
}
输出:
执行热重启... 正在将文件同步到设备Redmi Note 4 ... 在1,994毫秒内重新启动应用程序。 I / flutter(2257):W小工具库引起的异常CA ═════════════════════════ I / flutter(2257):构建StreamBuilder(脏,状态: 我/ Flutter(2257):_StreamBuilderBaseState>#94c3a): I / flutter(2257):'package:firebase_demo / main.dart':失败的断言:第86行pos 16:'map ['votes']!= null':不是 I / flutter(2257):是的。 I /颤振(2257): I / flutter(2257):要么断言表明框架本身有错误,要么我们应该提供 I / flutter(2257):此错误消息中的更多信息可帮助您确定和解决根本原因。 I / flutter(2257):无论哪种情况,请通过在GitHub上提交错误来报告此断言: I / flutter(2257):https://github.com/flutter/flutter/issues/new?template=BUG.md I /颤振(2257): I / flutter(2257):引发异常时,这是堆栈: I / flutter(2257):#2新Record.fromMap(package:firebase_demo / main.dart:86:16) I / flutter(2257):#3新Record.fromSnapshot(package:firebase_demo / main.dart:91:14) I / flutter(2257):#4 _MyHomePageState._buildListItem(package:firebase_demo / main.dart:59:27) I / flutter(2257):#5 _MyHomePageState._buildList。 (软件包:firebase_demo / main.dart:54:40) I / flutter(2257):#6 MappedListIterable.elementAt(dart:_internal / iterable.dart:414:29) I / flutter(2257):#7 ListIterable.toList(dart:_internal / iterable.dart:219:19) I / flutter(2257):#8 _MyHomePageState._buildList(package:firebase_demo / main.dart:54:71) I / flutter(2257):#9 _MyHomePageState._buildBody。 (软件包:firebase_demo / main.dart:46:16) I / flutter(2257):#10 StreamBuilder.build(软件包:flutter / src / widgets / async.dart:423:74) I / flutter(2257):#11 _StreamBuilderBaseState.build(包:flutter / src / widgets / async.dart:125:48) I / flutter(2257):#12 StatefulElement.build(package:flutter / src / widgets / framework.dart:3809:27) I / flutter(2257):#13 ComponentElement.performRebuild(package:flutter / src / widgets / framework.dart:3721:15) I / flutter(2257):#14 Element.rebuild(包:flutter / src / widgets / framework.dart:3547:5) I / flutter(2257):#15 BuildOwner.buildScope(package:flutter / src / widgets / framework.dart:2286:33) I / flutter(2257):#16 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame(package:flutter / src / widgets / binding.dart:676:20) I / flutter(2257):#17 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback(package:flutter / src / rendering / binding.dart:219:5) I / flutter(2257):#18 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback(包:flutter / src / scheduler / binding.dart:990:15) I / flutter(2257):#19 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame(package:flutter / src / scheduler / binding.dart:930:9) I / flutter(2257):#20 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame(package:flutter / src / scheduler / binding.dart:842:5) I / flutter(2257):#21 _invoke(dart:ui / hooks.dart:154:13) I / Flutter(2257):#22 _drawFrame(dart:ui / hooks.dart:143:3) I / flutter(2257):(从类_AssertionError中删除了2帧) I /颤振(2257):═══════════════════════════════════════════ ══════════════════════════════════════════════════ ═══════ I / 1.gpu(2257):类型= 1400审核(0.0:191179):AVC:拒绝{ioctl} for path =“ / dev / kgsl-3d0” dev =“ tmpfs” ino = 15394 ioctlcmd = 945 scontext = u :r:untrusted_app_27:s0:c512,c768 tcontext = u:object_r:device:s0 tclass = chr_file允许= 1 I / 1.gpu(2257):类型= 1400审核(0.0:191180):AVC:拒绝{读写}用于路径=“ / dev / kgsl-3d0” dev =“ tmpfs” ino = 15394 scontext = u:r :untrusted_app_27:s0:c512,c768 tcontext = u:object_r:device:s0 tclass = chr_file允许= 1 I / an.firebasedem(2257):等待阻塞的GC ProfileSaver I / an.firebasedem(2257):WaitForGcToComplete在ProfileSaver上阻止了ProfileSaver 30.771ms
答案 0 :(得分:0)
也许您可以先在Firebase中检查集合名称。
stream: Firestore.instance.collection('your_collection_name_here').snapshots(),
答案 1 :(得分:0)
确保您在Firebase中的字段与Record的属性相匹配。
例如: 名称与数据库中的名称字段匹配 投票与数据库中的投票字段匹配