当前位置: 首页 > 知识库问答 >
问题:

“Firebase/main.dart”:断言失败

范兴文
2023-03-14

我在flutter上使用firebase做了一个简单的婴儿名字项目,在成功完成教程后,该应用程序出现了这样的错误:“package:firebase_demo/main.dart':Failed assertion:line 86 pos 16:'map['votes]!=null':is not true。”

同样的帮助。

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

共有1个答案

商飞翮
2023-03-14

也许你可以先在火库里查一下你的收藏名。

stream: Firestore.instance.collection('your_collection_name_here').snapshots(),
 类似资料:
  • 我是JMeter和断言概念的新手。当我试图执行一个JMX文件(包含JMeter中的断言)时,遇到了以下错误消息: 断言错误:错误断言失败:真断言失败消息:测试失败:变量(搜索结果)不相等/接收:找不到[[[]]]]比较:找不到[[[]]]]] 脚本的执行方式如下:$java-jar./apache-jmeter-2.10/bin/apachejmeter.jar-t./jmeter-master/

  • 我正在使用JUnit自动化功能测试。我遇到了一个问题:如果我遵循规则“每个测试方法一个(重要的)断言”,那么每个测试用例最终会有一堆6行测试方法(17个是迄今为止最大的数字)。如果我将它们全部放入一个测试方法中,我必须注释掉失败的断言,否则一半的测试永远不会启动。 我不喜欢第一种方式,因为它启动浏览器的次数太多,而且浏览器启动登录/注销似乎比测试运行本身更“昂贵”和耗时。 第二种方法也不好,因为它

  • 问题内容: 我正在学习Xcode中的视图调试器,并通过 Debug > View Debugging> Capture View Hierarchy捕获视图层次结构 。但是,当我在应用程序中尝试该操作时,出现以下错误: -[UITextView _firstBaselineOffsetFromTop],/ BuildRoot / Library / Caches / com.apple.xbs /

  • 我在单元测试中使用groovy脚本。我有以下代码片段,我在单个测试脚本中使用多个断言。 第一个断言失败并停止执行。但我想继续进一步的代码片段。 与selenium中的软断言类似,我应该如何收集groovy中的所有失败异常。

  • 我正在努力学习树视图。但是,在运行代码时,我在执行一行时得到了一个错误- 可能的原因是什么? 编辑:当我在view类中编写AddressContentProvider类时,它工作得非常好。但是,我在其他文件中编写这个类,它不起作用:(

  • JUnit 5中是否有任何机制可以在测试中运行所有assert,即使中间的assert失败?例如: 我的意图是运行所有断言,并跟踪失败的只是第二个,而不是第三个和第一个。