0

下面的代码给出了子类型的错误

数据结构

代码

StreamBuilder(
        stream: _database.orderByKey().onValue,
        builder: (context, snapshot) {
          final valudisp = <Card>[];
          if (snapshot.hasData) {
            final firstdata = snapshot.data! as Map<String, dynamic>;
            firstdata.forEach((key, value) {
              final datalast = new Map<String, dynamic>.from(value);
              final coinname = datalast.keys;
              final displaycard = Card(...);
              valudisp.add(displaycard);
             });
          }
          return Expanded(...);
       }); 
      }    

现在我得到以下错误

type 'DatabaseEvent' is not a subtype of type 'Map<String,dynamic>' in typecast
4

2 回答 2

0

我认为您正在那里寻找DatabaseEvent课程

(snapshot.data! as DatabaseEvent).snapshot.value
                 // 
于 2022-01-16T18:56:24.430 回答
0

这段代码工作正常。

将第一个快照映射为 <Object, dynamic>。

StreamBuilder(
        stream: _database.onValue,
        builder: (context, snapshot) {
          final valudisp = <Card>[];
          if (snapshot.hasData) {
            final firstdata = (snapshot.data! as DatabaseEvent)
                .snapshot
                .value as Map<Object?, dynamic?>;
            firstdata.forEach((key, value) {
              final datalast = new Map<String, dynamic>.from(value);
              final coinname = datalast['namedata'];
              final _diff = datalast['dif'];
              final time = datalast['Time'];
              final Quantity = datalast['Quantity'];
              final tradep = datalast['tradep'];
              final displaycard =Card(...)
于 2022-01-26T07:45:01.740 回答