这里我遇到另一个提供商问题。 我有一个用于切换主视图的主窗口小部件,该窗口小部件称为switcher.dart
我将multiprovider内部的提供程序初始化为切换器,并在加载的视图中使用它(mainPage.dart)。该视图给了我找不到提供程序的错误。
switcher.dart
import '../providers/mainPageProvider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/userProvider.dart';
import 'package:flutter/widgets.dart';
import '../widgets/login.dart';
import '../widgets/mainPage.dart';
import '../widgets/splash.dart';
class Switcher extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProxyProvider<UserProvider, MainPageProvider>(
create: (context) => MainPageProvider.init(
Provider.of<UserProvider>(context, listen: false)),
update: (context, userProvider, mainPageProvider) =>
mainPageProvider..update(userProvider),
),
],
child: Consumer<UserProvider>(
builder: (context, user, _) {
print('switcher notifyed');
print(user.status);
switch (user.status) {
case UserStatus.Uninitialized:
return Splash();
break;
case UserStatus.Authenticated:
return MainPage();
break;
case UserStatus.Unauthenticated:
return Login();
break;
case UserStatus.Authenticating:
return Login();
break;
case UserStatus.Checking:
return Splash();
break;
default:
return Splash();
}
},
),
);
}
}
进入mainPage视图,我使用MainPageProvider的简单使用者,但说:
The following ProviderNotFoundError was thrown building Consumer<MainPageProvider>(dirty):
Error: Could not find the correct Provider<MainPageProvider> above this Consumer<MainPageProvider> Widget
To fix, please:
* Ensure the Provider<MainPageProvider> is an ancestor to this Consumer<MainPageProvider> Widget
* Provide types to Provider<MainPageProvider>
* Provide types to Consumer<MainPageProvider>
* Provide types to Provider.of<MainPageProvider>()
* Always use package imports. Ex: `import 'package:my_app/my_code.dart';
* Ensure the correct `context` is being used
view这种情况下的消费者示例mainPage.dart
import 'package:client/providers/mainPageProvider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:provider/provider.dart';
import '../widgets/smallCard.dart';
import 'contoCard.dart';
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
height: 100,
margin: EdgeInsets.only(left: 10),
child: Consumer<MainPageProvider>(
builder: (context, mainPageProvider, _) {
if(mainPageProvider.conti == null){
return SpinKitPulse(
color: Theme.of(context).primaryColor,
);
}else if(mainPageProvider.conti.length ==0){
return Text(
'Ancora nessun conto',
style: TextStyle(fontFamily: "sofiapro", fontSize: 14),
);
}else{
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: mainPageProvider.conti.length,
itemBuilder: (BuildContext context, index) {
var conti = mainPageProvider.conti;
var conto = conti[index];
return ContoCard(conto);
},
);
}
},
),
),
.......
对于我来说,切换器似乎是一个固定对象,因为视图是作为一个子视图加载的,所以我尝试从import'../providers/mainPageProvider.dart'更改导入方式;导入“ package:client / providers / mainPageProvider.dart”;但是错误仍然存在。
感谢您的帮助