gpt4 book ai didi

flutter - 在包含导航流的Flutter中,此Y小部件上方找不到正确的Provider

转载 作者:行者123 更新时间:2023-12-03 03:12:25 25 4
gpt4 key购买 nike

我不明白为什么在这里不接受提供者。

我的main.dart看起来像这样。

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ChangeNotifierProvider<ContentDS>(
builder: (_) => ContentDS(),
child: Content(),
)
);
}
}

Provider在Content Class中运行良好。
现在,我使用此代码从“内容”移至“内容详细信息”
Navigator.of(ctx).push(MaterialPageRoute(
builder: (context) =>
ContentDetails(viewedList: listElement),
))

在“内容详细信息”中,我只是尝试再次访问提供者
 final ds = Provider.of<ContentDS>(context);

但这给了我以下错误

错误:在此ContentDetails小部件上方找不到正确的提供程序

To fix, please:

  • Ensure the Provider is an ancestor to this ContentDetails Widget * Provide types to Provider * Provide types to Consumer * Provide types to Provider.of() * Always use package imports. Ex: import
    'package:my_app/my_code.dart'; * Ensure the correct
    context` is being used.


内容详细代码
class ContentDetails extends StatefulWidget {
ToDoList viewedList;

ContentDetails({this.viewedList});

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

class _ContentDetailsState extends State<ContentDetails> with SingleTickerProviderStateMixin{

bool isOpened = false;
AnimationController _animationController;
Animation<Color> _buttonColor;
Animation<double> _animateIcon;
Animation<double> _translateButton;
Curve _curve = Curves.easeOut;
double _fabHeight = 56.0;

initState() {
_animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500))
..addListener(() {
setState(() {});
});
_animateIcon =
Tween<double>(begin: 0.0, end: 1.0).animate(_animationController);
_buttonColor = ColorTween(
begin: Colors.blue,
end: Colors.red,
).animate(CurvedAnimation(
parent: _animationController,
curve: Interval(
0.00,
1.00,
curve: Curves.linear,
),
));
_translateButton = Tween<double>(
begin: _fabHeight,
end: -14.0,
).animate(CurvedAnimation(
parent: _animationController,
curve: Interval(
0.0,
0.75,
curve: _curve,
),
));
super.initState();
}

@override
dispose() {
_animationController.dispose();
super.dispose();
}

animate() {
if (!isOpened) {
_animationController.forward();
} else {
_animationController.reverse();
}
isOpened = !isOpened;
}

@override
Widget build(BuildContext context) {
final ds = Provider.of<ContentDS>(context);

print(" ds.count ---> " + ds.count.toString());
if (widget.viewedList != null){
debugPrint(" widget.viewedList.primaryID ---> " + widget.viewedList.primaryID);
}else{
debugPrint(" It is null");
}
return
Hero(
tag: (widget.viewedList != null) ? widget.viewedList.primaryID : 'Hero',
child: Scaffold(
appBar: new AppBar(
backgroundColor: ListStatusHelper.getBgGenericColor(widget.viewedList?.status ?? ListStatus.NONE),
title: Text('${getListTitle()}'),
elevation: 0.0,
),
floatingActionButton: Wrap(
direction: Axis.vertical,
children: <Widget>[
transformWorks(edit(), 3),
transformWorks(save(), 2),
transformWorks(inbox(),1),
toggleButton()
],
),
body: Container(
color: ListStatusHelper.getBgGenericColor(widget.viewedList?.status ?? ListStatus.NONE),
child: const Text("Once Provider is available, need to work on this section");

),
),
);
}

Widget transformWorks(Widget btn, int pos){
return Transform(
transform: Matrix4.translationValues(
0.0,
_translateButton.value * pos,
0.0,
),
child: btn,
);
}

Widget inbox() {
return Container(
child: FloatingActionButton(
heroTag: null,
onPressed: null,
tooltip: 'Inbox',
elevation: 0.0,
child: Icon(Icons.inbox),
),
);
}

Widget edit() {
return Container(
child: FloatingActionButton(
heroTag: null,
onPressed: null,
tooltip: 'Edit',
elevation: 0.0,
child: Icon(Icons.edit),
),
);
}

Widget save() {
return Container(
child: FloatingActionButton(
heroTag: null,
onPressed: null,
tooltip: 'Save',
elevation: 0.0,
child: Icon(Icons.save),
),
);
}

Widget toggleButton(){
return FloatingActionButton(
heroTag: null,
backgroundColor: _buttonColor.value,
onPressed: animate,
tooltip: 'Toggle',

child: AnimatedIcon(
icon: AnimatedIcons.menu_close,
progress: _animateIcon,
),
);
}

String getListTitle() {
if (widget.viewedList != null) {
return widget.viewedList.listTitle;
} else {
return 'New List';
}
}
}

我是一位试图学习抖动的iOS开发人员,我认为Provider应该可用于导航流程中的所有类,而无需使用任何DI。

帮助我意识到我在想什么。在诸如上述情况的导航流程中,应如何使用Provider。

提前致谢

最佳答案

ChangeNotifier移到MyApp之上:

void main() {
runApp(ChangeNotifierProvider(
builder: (context) => ContentDS(),
child: MyApp()));
}

关于flutter - 在包含导航流的Flutter中,此Y小部件上方找不到正确的Provider <X>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58499354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com