【问题标题】:Tried to use Provider with a subtype of Listenable/Stream尝试使用带有 Listenable/Stream 子类型的 Provider
【发布时间】:2021-10-19 00:02:58
【问题描述】:

我正在尝试制作 MultiProvider。我不知道错误在哪里。我是新的 Dart 和 Flutter。尝试自己修复它,但无法修复它。当我运行项目时,它卡在错误屏幕上。我附上问题的图片。

This pic shows error display on emulator

这是我的代码 main.dart 文件

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:vendorapp/providers/auth_providers.dart';
import 'package:vendorapp/screens/homescreen.dart';
import 'package:vendorapp/screens/registerscreen.dart';
import 'package:vendorapp/screens/splashscreen.dart';

void main() async {
  //Provider.debugCheckInvalidValueType = null;
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp( MultiProvider(
      providers:[
        Provider (create: (_) => AuthProvider()),
      ],
    child: MyApp(),
  )
  );
}

class MyApp extends StatelessWidget {
  const MyApp({key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primaryColor: Colors.greenAccent,
        fontFamily: 'Poppins',
      ),
      initialRoute: SplashScreen.id,
      routes: {
        SplashScreen.id: (context) => SplashScreen(),
        RegisterScreen.id: (context) => RegisterScreen(),
        HomeScreen.id: (context) => HomeScreen(),
      },
    );
  }
}

这是我的 image_picker.dart 文件

import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';
import 'package:vendorapp/providers/auth_providers.dart';

class ShopPicCard extends StatefulWidget {

  const ShopPicCard({Key? key}) : super(key: key);

  @override
  _ShopPicCardState createState() => _ShopPicCardState();
}

class _ShopPicCardState extends State<ShopPicCard> {
  @override
  Widget build(BuildContext context) {
    final _authData = Provider.of<AuthProvider>(context);
    File? _image;
    return Padding(
      padding: const EdgeInsets.all(20.0),
      child: InkWell(
        onTap: (){
          _authData.getImage().then((image){
            setState(() {
              _image = image;
            });
            if(image!=null){
              _authData.isPicAvail = true;
            }
          });
        },
        child: SizedBox(
          height: 150,
          width: 150,
          child: Card(
            child: _image == null ? Center(child: Text('Add Shop Image',
              style: TextStyle(color: Colors.greenAccent),)
            ):Image.file(_image!,fit: BoxFit.fill,),
          ),
        ),
      ),
    );
  }
}

    ```


Here are the error Logs

在为 x86 构建的 Android SDK 上以调试模式启动 lib\main.dart... 正在运行 Gradle 任务“assembleDebug”... 参数格式不正确 - √ 内置 build\app\outputs\flutter-apk\app-debug.apk。 正在安装 build\app\outputs\flutter-apk\app.apk... D/FlutterLocationService(27889):创建服务。 D/FlutterLocationService(27889):绑定到位置服务。 D/LocationPlugin(27889):服务连接:ComponentInfo{com.example.vendorapp/com.lyokone.location.FlutterLocationService} D/EGL_emulation(27889): eglMakeCurrent: 0x9e705ea0: ver 2 0 (tinfo 0x9e703810) 在 ws://127.0.0.1:58245/8VdeXtRD7tk=/ws 上侦听的调试服务 将文件同步到为 x86 构建的设备 Android SDK... I/zygote (27889):做部分代码缓存收集,代码=29KB,数据=21KB I/zygote (27889):代码缓存收集后,代码=29KB,数据=21KB I/zygote (27889):将代码缓存容量增加到 128KB D/EGL_emulation(27889): eglMakeCurrent: 0x9e6909e0: ver 2 0 (tinfo 0x9e7036a0)

======== 小部件库捕获的异常=================================== ====================== 在构建 ShopPicCard(dirty, dependencies: [_InheritedProviderScope], state: _ShopPicCardState#396e1) 时抛出了以下断言: 尝试使用带有 Listenable/Stream (AuthProvider) 子类型的 Provider。

这很可能是一个错误,因为提供者不会自动更新家属 当 AuthProvider 更新时。相反,请考虑将 Provider 更改为更具体的 处理更新机制的实现,例如:

  • ListenableProvider
  • ChangeNotifierProvider
  • ValueListenableProvider
  • 流提供者

或者,如果您正在创建自己的提供程序,请考虑使用 InheritedProvider。

如果您认为这不是错误,您可以通过设置禁用此检查 Provider.debugCheckInvalidValueType 到你的主文件中的null

void main() {
  Provider.debugCheckInvalidValueType = null;

  runApp(MyApp());
}
The relevant error-causing widget was: 
  ShopPicCard file:///C:/Users/The%20Cubix/StudioProjects/vendorapp/lib/screens/registerscreen.dart:15:15
When the exception was thrown, this was the stack: 
#0      Provider.debugCheckInvalidValueType.<anonymous closure>.<anonymous closure> (package:provider/src/provider.dart:376:9)
#1      Provider.debugCheckInvalidValueType.<anonymous closure> (package:provider/src/provider.dart:403:6)
#2      new Provider.<anonymous closure> (package:provider/src/provider.dart:215:56)
#3      _CreateInheritedProviderState.value.<anonymous closure> (package:provider/src/inherited_provider.dart:700:48)
#4      _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:702:10)
...
====================================================================================================

======== Exception caught by rendering library =====================================================
The following assertion was thrown during layout:
A RenderFlex overflowed by 99203 pixels on the bottom.

The relevant error-causing widget was: 
  Column file:///C:/Users/The%20Cubix/StudioProjects/vendorapp/lib/screens/registerscreen.dart:13:18
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.

Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.

The specific RenderFlex in question is: RenderFlex#17888 relayoutBoundary=up2 OVERFLOWING
...  parentData: offset=Offset(0.0, 0.0) (can use size)
...  constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=796.6)
...  size: Size(411.4, 796.6)
...  direction: vertical
...  mainAxisAlignment: start
...  mainAxisSize: max
...  crossAxisAlignment: center
...  verticalDirection: down
  child 1: RenderErrorBox#69883
    parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
    constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=Infinity)
    size: Size(411.4, 100000.0)
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
====================================================================================================

【问题讨论】:

    标签: flutter dart flutter-provider


    【解决方案1】:

    在您的 main 方法和您的 runApp 调用中。尝试替换

    Provider(create: (_) => AuthProvider()),
    

    与适当的提供者合作。

    如果AuthProviderStream,请使用StreamProvider

    StreamProvider(create: (_) => AuthProvider()),
    

    如果AuthProviderChangeNotifier,请使用ChangeNotifierProvider

    ChangeNotifierProvider(create: (_) => AuthProvider()),
    

    如果AuthProvider 是另一种类型的可侦听对象(我无法从您提供的代码中看出),请尝试将Provider 替换为正确的类型(ListenableProviderValueListenableProvider)。

    【讨论】:

    • 感谢您的重播,但如果看到我正在使用的主 dart 文件 runApp( MultiProvider( providers:[ Provider (create: (_) => AuthProvider()), ], child: MyApp( ), )
    • 是的,所以尝试用我在回答中解释的内容修改这一行。例如ChangeNotifierProviderrunApp( MultiProvider( providers:[ ChangeNotifierProvider (create: (_) =&gt; AuthProvider()), ], child: MyApp(), )
    • 非常感谢,伙计,它现在可以正常工作了。你拯救了我的一天。
    • 不客气,别忘了验证答案以帮助未来的读者;)
    • AuthProvider 仍然会发生这种情况,还是其他提供商?如果是,您是否确定您使用了正确的提供商? (StreamProviderChangeNotifierProvider)
    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 2020-10-23
    • 2020-07-02
    • 1970-01-01
    • 2020-10-22
    • 2020-11-06
    • 1970-01-01
    • 2020-10-14
    相关资源
    最近更新 更多