引发了另一个异常:类型'_MapStream <QuerySnapshot,List <AccountSummary >>'不是类型'List <AccountSummary>'的子类型

时间:2020-09-01 17:11:05

标签: firebase flutter dart stream

这是用于消防站的服务

  Stream<List<AccountSummary>> get accountsStream {
    CollectionReference _accountReference = FirebaseFirestore.instance
        .collection('borrowers')
        .doc(uid)
        .collection('accounts');
    return _accountReference.snapshots().map((snapshot) => snapshot.docs
        .map((document) => AccountSummary.fromMap(document.data()))
        .toList());
  }

这是小部件代码

  Widget build(BuildContext context) {
    final borrowerService =
        Provider.of<BorrowerService>(context, listen: false);
    final List<AccountSummary> accountlist =
        borrowerService.accountsStream ?? [];
    print(accountlist.length);
    return StreamBuilder<List<AccountSummary>>(
      stream: borrowerService.accountsStream,
      builder: (context, AsyncSnapshot<List<AccountSummary>> snapshot) {
        final List<AccountSummary> summary = snapshot.data;
        if (summary != null) {
          return ListView.builder(
            itemCount: summary.length,
            itemBuilder: (context, int index) {
              return AccountTile(summary[index]);
            },
          );
        }
        return Center(
          child: Text('Loading'),
        );
      },
    );
  }

我在UI上收到此错误消息

引发了另一个异常:类型'_MapStream '不是类型'List'的子类型 我该如何修复代码?

这是我在调试控制台中遇到的错误,我在面向对象的编程中工作不多,我认为这与没有获得所需的类有关,或者请帮忙

═╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building AccountList(dirty):
type '_MapStream<QuerySnapshot, List<AccountSummary>>' is not a subtype of type
'List<AccountSummary>'
The relevant error-causing widget was:
  AccountList
  
lib\…\Borrowings\borrow_profile_screen.dart:66
When the exception was thrown, this was the stack:
#0      AccountList.build 
package:paisa/widgets/account_list.dart:12
#1      StatelessElement.build 
package:flutter/…/widgets/framework.dart:4620
#2      ComponentElement.performRebuild 
package:flutter/…/widgets/framework.dart:4546

#564    ComponentElement._firstBuild 
package:flutter/…/widgets/framework.dart:4525
#565    ComponentElement.mount 
package:flutter/…/widgets/framework.dart:4520
...     Normal element mounting (7 frames)
#572    SingleChildWidgetElementMixin.mount 
package:nested/nested.dart:223
...     Normal element mounting (7 frames)
#579    _NestedHookElement.mount 
package:nested/nested.dart:188
...     Normal element mounting (7 frames)
#586    SingleChildWidgetElementMixin.mount 
package:nested/nested.dart:223
#587    Element.inflateWidget 
package:flutter/…/widgets/framework.dart:3490
#588    Element.updateChild 
package:flutter/…/widgets/framework.dart:3255
#589    ComponentElement.performRebuild 
package:flutter/…/widgets/framework.dart:4571
#590    StatefulElement.performRebuild 
package:flutter/…/widgets/framework.dart:4719
#591    Element.rebuild 
package:flutter/…/widgets/framework.dart:4262
#592    BuildOwner.buildScope 
package:flutter/…/widgets/framework.dart:2667
#593    WidgetsBinding.drawFrame 
package:flutter/…/widgets/binding.dart:866
#594    RendererBinding._handlePersistentFrameCallback 
package:flutter/…/rendering/binding.dart:286
#595    SchedulerBinding._invokeFrameCallback 
#596    SchedulerBinding.handleDrawFrame 
package:flutter/…/scheduler/binding.dart:1056
#597    SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> 
package:flutter/…/scheduler/binding.dart:865
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════
The following _TypeError was thrown building AccountList(dirty):
type '_MapStream<QuerySnapshot, List<AccountSummary>>' is not a subtype of type 'List<AccountSummary>'

The relevant error-causing widget was
    AccountList 
lib\…\Borrowings\borrow_profile_screen.dart:66
When the exception was thrown, this was the stack
#0      AccountList.build 
package:paisa/widgets/account_list.dart:12
#1      StatelessElement.build 
package:flutter/…/widgets/framework.dart:4620
#2      ComponentElement.performRebuild 
package:flutter/…/widgets/framework.dart:4546
#3      Element.rebuild 
package:flutter/…/widgets/framework.dart:4262
#4      ComponentElement._firstBuild 
package:flutter/…/widgets/framework.dart:4525
...
═══════ Exception caught by widgets library ═══════════════════════════════════
type '_MapStream<QuerySnapshot, List<AccountSummary>>' is not a subtype of type 'List<AccountSummary>'
The relevant error-causing widget was
    AccountList 
lib\…\Borrowings\borrow_profile_screen.dart:66
════════════════════════════════════════════════════════════════════════════════

1 个答案:

答案 0 :(得分:0)

我发现了代码问题

    final List<AccountSummary> accountlist =
        borrowerService.accountsStream ?? [];
    print(accountlist.length);

这段代码就是问题所在。当我删除它时。它开始工作。

以前,我曾尝试在没有StreamBuild的情况下构建窗口小部件,却忘记了干净的代码。