简体   繁体   中英

'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. "

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. 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). 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). 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. (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 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 ="/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

Maybe you can check your collection name in the firebase first.

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

Make sure your fields in the Firebase match with the proprieties of Record.

eg: name matches name field from the database votes matches votes field from the database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM