flutter - 找不到正确的 Provider 。您尝试阅读的提供者处于不同的路线
问题描述
我的应用程序中有以下路线:
Main.dart ---> SplashScreen.dart ---> DetailsPage.dart
主要.dart
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => FontSizeHandler()),
],
child: MyApp(),
),
);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SplashScreen(),
);
}
}
从SplashScreen.dart我移动到DetailsPage.dart使用 Navigator.pushAndRemoveUntil即
Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => DetailsPage()), (route) => false);
现在在App Bar 的详细信息页面中,有一个图标,按下它我想使用FontSizeHandler更改字体
详细信息页面.dart
class DetailsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
icon: Icon(Icons.arrow_upward),
onPressed: () {
context.read<FontSizeHandler>().increaseFont();
},
),
IconButton(
onPressed: () {
context.read<FontSizeHandler>().decreaseFont();
},
icon: Icon(Icons.arrow_downward),
),
],
title: Text(
"DetailsPage",
style: GoogleFonts.roboto(),
),
),
body: SafeArea(
child: SingleChildScrollView(
child: Card(
child: Container(
padding: EdgeInsets.fromLTRB(5, 5, 5, 15),
child: AutoSizeText(
"MyTexts",
textAlign: TextAlign.justify,
style: GoogleFonts.openSans(
fontSize:
context.watch<FontSizeHandler>().fontSize.toDouble(),
),
),
),
),
),
),
);
}
}
所以这里的问题是我收到了这个错误信息
找不到正确的提供程序 这可能是因为您使用了
BuildContext
不包含提供程序的
这个错误是因为我使用了Navigator.pushAndRemoveUntil吗?虽然我在层次结构顶部有ChangeNotifierProvider,但它为什么会抛出错误?如何解决这个问题?
字体大小处理程序.dart
class FontSizeHandler with ChangeNotifier {
int fontSize = 15;
void increaseFont() {
fontSize = fontSize + 2;
notifyListeners();
}
void decreaseFont() {
fontSize = fontSize - 2;
notifyListeners();
}
}
已解决:问题在于导入错误的 ChangeNotifier 类。再也不相信自动导入
解决方案
更新的答案
作为参考,该问题是由以下错误导入引起的:
import 'file:///.../fontchangehandler.dart'; // import 'package:.../fontchangehandler.dart';
原始答案
不幸的是,我无法重现您使用提供的代码遇到的错误。如果您可以提供DetailsPage
该类的其余代码,那么这可能有助于进一步诊断错误。我能够使下面的示例正常工作,希望您会发现它有用:
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<FontSizeHandler>(
create: (context) {
return FontSizeHandler();
},
),
],
child: MyApp(),
),
);
}
class FontSizeHandler extends ChangeNotifier {
int _fontSize = 15;
int get fontSize => _fontSize;
void increaseFont() {
_fontSize = _fontSize + 2;
notifyListeners();
}
void decreaseFont() {
_fontSize = _fontSize - 2;
notifyListeners();
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SplashScreen(),
);
}
}
class SplashScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () async {
await Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) {
return DetailsPage();
},
),
(route) => false,
);
},
child: Text('GO TO DETAILS'),
),
),
);
}
}
class DetailsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
icon: Icon(Icons.arrow_upward),
onPressed: () {
context.read<FontSizeHandler>().increaseFont();
},
),
IconButton(
onPressed: () {
context.read<FontSizeHandler>().decreaseFont();
},
icon: Icon(Icons.arrow_downward),
),
],
title: Text(
"DetailsPage",
style: GoogleFonts.roboto(),
),
),
body: SafeArea(
child: SingleChildScrollView(
child: Card(
child: Container(
padding: EdgeInsets.fromLTRB(5, 5, 5, 15),
child: AutoSizeText(
"MyTexts",
textAlign: TextAlign.justify,
style: GoogleFonts.openSans(
fontSize: context.watch<FontSizeHandler>().fontSize.toDouble(),
),
),
),
),
),
),
);
}
}
推荐阅读
- go - 如何将任意 proto 消息作为输入并用它调用 protojson.Marshal
- javascript - 为什么我的 JavaScript 对象函数将其参数名称分配为键而不是参数名称?
- python - Django外键关系views.py和模板html
- ios - 无法使用包含 pod 的 xcframework 发布应用程序
- amazon-web-services - EC2 Meteor 部署上的 Web 连接被拒绝
- python - 使用基于(类)的代码反转字符串
- scala - 将字符串“decimal”转换为 DecimalType
- sql - 如何使用带有子句定义的两列 oracle 文本
- python - 如何检查矩阵的某些元素并相应地生成一组矩阵?
- google-sheets - 为什么 ASTERISK 在谷歌表格中不起作用?