Flutter:无法从 firebase 读取数据。方法 '[]' 被调用为 null 错误

Flutter: Unable to read data from firebase. The method '[]' was called on null error

我正在尝试在扫描条形码后从 firebase 读取数据。

它应该是这样显示的,但它应该显示数据库中的名称和价格而不是条形码 (https://m.imgur.com/gallery/lEFJZ0Q)

代码:

class ListTileModel {
  String barcode;
  ListTileModel(this.barcode);
}

下面的代码在有状态小部件中

List<ListTileModel> _items = [];
String barcode = "";

  void _add() {
    _items.add(ListTileModel(barcode));
      setState(() {});
  }


  @override
  void initState(){
    super.initState();
  }

使用的 StreamBuilder:

new Container(

              child: ListView(
                  children: _items
                      .map((item) => StreamBuilder(
                  stream: FirebaseDatabase.instance.reference().child('prd').child(item.barcode).onValue,
                  builder: (context, snap) {
                    print(item.barcode);
                    if(snap.hasData){
                      DataSnapshot snapshot = snap.data.snapshot;
                      Map<dynamic, dynamic> itm = snapshot.value;
                      return snap.data.snapshot.value == null
                          ? SizedBox()
                          : ListView.builder(
                        shrinkWrap: true,
                        scrollDirection: Axis.vertical,
                        itemCount: 1,
                        itemBuilder: (context, index) {
                          return Row(
                            children: <Widget>[

                              ConstrainedBox(

                                child: Container(

                                  child: Text(itm[index]['name'],style: TextStyle(fontSize: 20),),
                                ),
                              ),
                              SizedBox(width: 100,),
                              ConstrainedBox(

                                child: Container(

                                  child: Text(itm[index]['price'].toString(),style: TextStyle(fontSize: 20),),
                                ),
                              ),
                            ],
                          );
                        },
                      );
                    }
                    else {
                      return   Center(child: CircularProgressIndicator());
                    }
                  }
              ),
                  ).toList()
              ),
            ),

条码扫描码:

小部件:

floatingActionButton: FloatingActionButton(
          onPressed: scan,
        child: Icon(Icons.add_shopping_cart),

      ),

扫描() :

Future scan() async{
    try{
      String barcode = await BarcodeScanner.scan();
        setState(() {
          this.barcode = barcode;
        });
      _add();
    }on PlatformException catch(e) {
      if(e.code == BarcodeScanner.CameraAccessDenied){
        setState(() {
          this.barcode = 'Access Denied';
        });
      }
    }

我遇到以下错误:

构建时抛出了以下 NoSuchMethodError: 在 null 上调用了方法“[]”。 接收者:空 尝试调用:

请试试这个,如果它解决了您需要更改的问题,请告诉我

Text(itm[index]['name'],style: TextStyle(fontSize: 20),),

Text(itm[index]['price'].toString(),style: TextStyle(fontSize: 20),),

Text(itm['det']['name'],style: TextStyle(fontSize: 20),),

Text(itm['det']['price'].toString(),style: TextStyle(fontSize: 20),),

让我知道这是否适合您。我相信问题也是索引。 现在你的话。