错误状态:尝试读取在创建其值期间抛出的提供程序。在创建 Future<List<Place>>

时间:2021-05-03 13:04:07

标签: android flutter dart provider flutter-provider

我知道很多人问这个问题,但我没有找到任何答案,所以这有点不同

我正面临这个错误。当我直接从主页调用 Desired Screen 时它工作正常,但是当我从另一个屏幕导航 DesiredScreen 时,它显示此错误。我用地图给你解释

================================================ ============

当我这样导航时没问题

MultiProvider -> MaterialApp -> DesiredScreen

================================================ ============

但是当我浏览它时,它显示了这个 BadState 错误

MultiProvider -> MaterialApp -> HomePage -> DesiredScreen

================================================ ============

更新的问题

当我把条件放在主类小部件中时

count == 1 ? DesiredScreen() : HomePage()

它显示此错误,但如果我从其他屏幕导航到 DesiredScreen,它不会显示错误

================================================ ============

代码

Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        FutureProvider(
          create: (_) => locatorService.getLocation(),
          initialData: null,
        ),
        FutureProvider(
          initialData: null,
          create: (_) {
            ImageConfiguration imageConfiguration =
                createLocalImageConfiguration(context);
            return BitmapDescriptor.fromAssetImage(
                imageConfiguration, 'assets/images/fire.png');
          },
        ),
        ProxyProvider2<Position, BitmapDescriptor, Future<List<Place>>>(
            update: (_, position, icon, places) {
          return (position != null)
              ? placeService.getPlaces(
                  position.latitude, position.longitude, icon)
              : null;
        })
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primaryColor: Colors.blueAccent,
        ),
        home: DesiredScreen(),

        
      ),
    );

错误

    ════════ Exception caught by widgets library ═══════════════════════════════════
    The following StateError was thrown building GoogleMapView(dirty, dependencies: [_InheritedProviderScope<Position>, _InheritedProviderScope<Future<List<Place>>>], state: _GoogleMapViewState#878c8):
    Bad state: Tried to read a provider that threw during the creation of its value.
    The exception occurred during the creation of type Future<List<Place>>.
    
    The relevant error-causing widget was
    GoogleMapView
    lib\screens\home.dart:7
    When the exception was thrown, this was the stack
    #0      _CreateInheritedProviderState.value
    package:provider/src/inherited_provider.dart:661
    #1      _InheritedProviderScopeElement.value
    package:provider/src/inherited_provider.dart:544
    #2      Provider.of
    package:provider/src/provider.dart:289
    #3      _GoogleMapViewState.build
    package:vibecheck/screens/google_map_view.dart:39
    #4      StatefulElement.build
    package:flutter/…/widgets/framework.dart:4612
    ...
    ════════════════════════════════════════════════════════════════════════════════

0 个答案:

没有答案