简体   繁体   English

'firebase/main.dart':断言失败

[英]'firebase/main.dart': Failed assertion

I have made a simple baby names project using firebase on flutter, after successfully going throught the tutorial, the app gives this error: " package:firebase_demo/main.dart': Failed assertion: line 86 pos 16: 'map['votes]: = null'. is not true. "我在 flutter 上使用 firebase 做了一个简单的婴儿名字项目,在成功完成本教程后,应用程序给出了这个错误:“ package:firebase_demo/main.dart': Failed assertion: line 86 pos 16: 'map['votes] : = null'. 不是真的。”

Help for the same.帮助相同。

MY CODE -:我的代码-:

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>";
}

Output:输出:

Performing hot restart... Syncing files to device Redmi Note 4... Restarted application in 1,994ms.执行热重启...正在将文件同步到设备 Redmi Note 4...在 1,994 毫秒内重新启动应用程序。 I/flutter ( 2257): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 2257): The following assertion was thrown building StreamBuilder(dirty, state: I/flutter ( 2257): _StreamBuilderBaseState>#94c3a): I/flutter ( 2257): 'package:firebase_demo/main.dart': Failed assertion: line 86 pos 16: 'map['votes']:= null': is not I/flutter ( 2257). I/flutter ( 2257 ): ══╡ 小部件库异常 ╞═════════════════════════════════════ ═════════════════════════ I/flutter ( 2257 ): 以下断言被抛出构建 StreamBuilder( 脏,状态: I/flutter ( 2257 ) : _StreamBuilderBaseState>#94c3a): I/flutter ( 2257): 'package:firebase_demo/main.dart': Failed assertion: line 86 pos 16: 'map['votes']:= null': is not I/flutter ( 2257). true: I/flutter ( 2257): I/flutter ( 2257), Either the assertion indicates an error in the framework itself: or we should provide substantially I/flutter ( 2257). true: I/flutter ( 2257): I/flutter ( 2257), 要么断言表明框架本身有错误:要么我们应该提供实质性的 I/flutter ( 2257)。 more information in this error message to help you determine and fix the underlying cause: I/flutter ( 2257), In either case: please report this assertion by filing a bug on GitHub: I/flutter ( 2257): https://github.com/flutter/flutter/issues/new?template=BUG.md I/flutter ( 2257): I/flutter ( 2257): When the exception was thrown, this was the stack: I/flutter ( 2257): #2 new Record.fromMap (package:firebase_demo/main.dart:86:16) I/flutter ( 2257): #3 new 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.此错误消息中的更多信息可帮助您确定和修复根本原因:I/flutter ( 2257),无论哪种情况:请通过在 GitHub 上提交错误来报告此断言:I/flutter ( 2257): https://github .com/flutter/flutter/issues/new?template=BUG.md I/flutter ( 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。 (package: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. (package: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 . (package:firebase_demo/main.dart:46:16) I/flutter ( 2257): #10 StreamBuilder.build (package:flutter/src/widgets/async.dart:423:74) I/flutter ( 2257): #11 _StreamBuilderBaseState.build (package: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 (package: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&Rend (package:firebase_demo/main.dart:46:16) I/flutter (2257):#10 StreamBuilder.build (package:flutter/src/widgets/async.dart:423:74) I/flutter (2257):# 11 _StreamBuilderBaseState.build (package: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(包: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&BindingWidgetingsInding&Rmantics (包:flutter/src/widgets/framework.dart:2286:33) #16 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&BindingWendersBinding&Rmantics包:flutter/src/widgets/binding.dart:676:20) I/flutter ( 2257): #17 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&Rend ererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5) I/flutter ( 2257): #18 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package: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): (elided 2 frames from class _AssertionError) I/flutter ( 2257): ════════════════════════════════════════════════════════════════════════════════════════════════════ I/1.gpu ( 2257): type=1400 audit(0.0:191179): avc: denied { ioctl } for path ererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5) I/flutter (2257): #18 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990) 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/flutter ( 2257): ═════════════════════════ ══════════════════════════════════════════════════ ═════════════════════════ I/1.gpu (2257): type=1400 audit(0.0:191179): avc: denied { ioctl }对于路径="/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 permissive=1 I/1.gpu ( 2257): type=1400 audit(0.0:191180): avc: denied { read write } for path="/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 permissive=1 I/an.firebasedem( 2257): Waiting for a blocking GC ProfileSaver I/an.firebasedem( 2257): WaitForGcToComplete blocked ProfileSaver on ProfileSaver for 30.771ms ="/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 permissive=1 I/ 1.gpu (2257): type=1400 audit(0.0:191180): avc: denied { read write } for path="/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 permissive=1 I/an.firebasedem( 2257): 等待阻塞 GC ProfileSaver I/an.firebasedem( 2257): WaitForGcToComplete 在 ProfileSaver 上阻塞 ProfileSaver 30.771 毫秒

Maybe you can check your collection name in the firebase first.也许你可以先在 firebase 中检查你的集合名称。

stream: Firestore.instance.collection('your_collection_name_here').snapshots(),

Make sure your fields in the Firebase match with the proprieties of Record.确保您在 Firebase 中的字段与 Record 的属性相匹配。

eg: name matches name field from the database votes matches votes field from the database例如:名称匹配数据库中的名称字段投票匹配数据库中的投票字段

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 If 语句添加到 main.dart 文件 - How Add a If statement to main.dart file 在 main.dart 中将 Future 与 MaterialApp.router 结合使用 - Use Future with MaterialApp.router in main.dart Flutter Navigator.of(context).pushReplacementNamed 抛出:“未处理的异常:Null 检查运算符用于 null 值”在 main.dart - Flutter Navigator.of(context).pushReplacementNamed throws :"Unhandled Exception: Null check operator used on a null value" on main.dart 未处理的异常:“package:firebase_storage/src/storage_reference.dart”:断言失败:第 62 行 pos 12:“file.existsSync()”:不是真的 - Unhandled Exception: 'package:firebase_storage/src/storage_reference.dart': Failed assertion: line 62 pos 12: 'file.existsSync()': is not true Firebase - 使用 Jest 进行测试会抛出错误“内部断言失败:需要一个 class 定义” - Firebase - Testing with Jest throws error "INTERNAL ASSERTION FAILED: Expected a class definition" 我无法部署 firebase 云功能:无法从源加载 function 定义,D:\ 中没有定义“导出”主要内容 - I cannot deploy firebase cloud functions: Failed to load function definition from source, No "exports" main defined in D:\ Dart 快照生成器失败,退出代码为 -9 - Dart snapshot generator failed with exit code -9 Firebase 函数部署失败 - Firebase functions failed to deploy Firebase 手机登录失败 - Firebase login failed on mobile Flutter:断言失败:'file.absolute.existsSync()':不正确 - Flutter: Failed assertion: 'file.absolute.existsSync()': is not true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM