我正在尝试构建具有备份和还原功能的Notes应用程序。我有一个打开应用程序时显示的主页。该页面的主体是一个脚手架,该脚手架又具有一个带有ListTiles的抽屉,用于备份和还原。我使用HomeBloc对象与保存注释的数据库进行交互,因此我使用Provider可以在任何地方访问它。
ListTiles打开MaterialPageRoute进入新屏幕,在该屏幕中,提示用户选择文件,输入密码等。
当我点击抽屉中的Restore ListTile时,出现此错误:
The following ProviderNotFoundException was thrown building RestoreLocalBackupPage(dirty, state: _RestoreLocalBackupPageState#4f937):
Error: Could not find the correct Provider<HomeBloc> above this RestoreLocalBackupPage Widget
This likely happens because you used a `BuildContext` that does not include the provider
of your choice.
这是我的main.dart,我将主页包装在提供程序中:
void main() {
runApp(
MyApp()
);
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Notes',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Provider(
create: (_) => HomeBloc(),
child: HomePage(),
)
);
}
}
这是我的主页的构建方法:
Widget build(BuildContext context) {
homeBloc = Provider.of<HomeBloc>(context);
return Scaffold(
backgroundColor: Color.fromRGBO(219, 243, 250, 1),
appBar: AppBar(...),
body: StreamBuilder<List<Note>>(...),
floatingActionButton: FloatingActionButton(...),
drawer: HomeDrawer(),
);
}
HomeDrawer的build方法返回一个Drawer,该子项具有一个ListView作为其子级。这是启动“还原备份”页面的ListTile的代码:
ListTile(
title: Text('Local Backup',
style: GoogleFonts.sourceSansPro(
textStyle: TextStyle(fontWeight: FontWeight.w500),
fontSize: 16)),
onTap: () async {
// Update the state of the app
// ...
// Then close the drawer
bool permissionGranted = await _getPermissions(context);
if (permissionGranted) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => CreateLocalBackupPage(
currentBackupDirectory: currentBackupDirectory
),
)
);
}
},
)
这是我点击上面的ListTile时遇到的错误:
The following ProviderNotFoundException was thrown building RestoreLocalBackupPage(dirty, state: _RestoreLocalBackupPageState#4f937):
Error: Could not find the correct Provider<HomeBloc> above this RestoreLocalBackupPage Widget
This likely happens because you used a `BuildContext` that does not include the provider
of your choice.
HomeDrawer()的BuildContext确实可以访问我需要的HomeBloc对象。因此,将RestoreLocalBackupPage小部件包装在另一个Provider中是可行的:
HomeBloc homebloc = Provider.of<HomeBloc>(context);
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Provider(
create: (_) => homebloc,
child: RestoreLocalBackupPage(currentBackupDirectory: currentBackupDirectory),
)
)
);
我想知道是否存在使用提供程序在RestoreLocalBackupPage内部访问HomeBloc的更简单,更优雅的方法。通过构造函数进行的依赖注入(inject)是可行的,但这种方法首先破坏了使用Provider的目的,对吗?
最佳答案
用提供程序包装main.dart中的MaterialApp解决了我的问题。 I have found the solution here. Check rrousselGit's answer.
之后,main.dart现在变为:
void main() {
runApp(
MyApp()
);
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
LicenseRegistry.addLicense(() async* {
final license = await rootBundle.loadString('google_fonts/Cabin_OFL.txt');
yield LicenseEntryWithLineBreaks(['google_fonts_Cabin'], license);
});
LicenseRegistry.addLicense(() async* {
final license = await rootBundle.loadString('google_fonts/SSP_OFL.txt');
yield LicenseEntryWithLineBreaks(['google_fonts_SSP'], license);
});
return Provider(
create: (_) => HomeBloc(),
child: MaterialApp(
title: 'Notes',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
),
);
}
}
关于flutter - 在此RestoreLocalBackupPage小部件上方找不到正确的Provider <HomeBloc>,如何以比我更简单的方式解决此问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62349890/