最近学习flutter,进行mvvm模式学习的时候,报了一错:
[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: Error: Could not find the correct Provider<MvvmDemoViewmodel> above this MvvmDemoView Widget
This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
- You added a new provider in your `main.dart` and performed a hot-reload.
To fix, perform a hot-restart.
- The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.
- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
Make sure that MvvmDemoView is under your MultiProvider/Provider<MvvmDemoViewmodel>.
This usually happens when you are creating a provider and trying to read it immediately.
For example, instead of:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// Will throw a P<…>
网上说的方法都试了,没丁点儿用,该报错还是报错。
后面我一步一步的看,终于:
原来是引的包,一个字母大写了。
所以大家如果遇到和我一样的错误,一定要先检查一下,包的引入对不对,以免走弯路。
以此为鉴,没有下次。