英文:
Providers in flutter
问题
我正在构建一个包含标题和描述详细信息的笔记应用,其中我的输入来自于弹出的AlertDialog中的TextField。不幸的是,屏幕是空白的。
这是我的UserNotes类:
class UserNotes {
String? title;
String? description;
UserNotes({required this.title, required this.description});
}
这是我的Provider:
import 'package:notes_update/models/UserMethods.dart';
import 'package:notes_update/models/UserNotes.dart';
import 'package:flutter/foundation.dart';
import 'package:provider/provider.dart';
class ProviderData extends ChangeNotifier {
List<UserNotes> _users = [];
List<UserNotes> get newusers => _users;
IncomingData() {
_users = GettingNotes().addDetails();
notifyListeners();
}
}
这个类返回一个UserNotes列表。
import 'UserNotes.dart';
class GettingNotes {
String? newtitle;
String? description;
List<UserNotes> addDetails() {
List<UserNotes> details = [];
if (details.isEmpty) {
details.add(UserNotes(title: newtitle!, description: description!));
}
return details;
}
}
这是我的UI:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:notes_update/models/UserMethods.dart';
import 'package:notes_update/models/UserNotes.dart';
import 'package:provider/provider.dart';
import '../Provider/Provder.dart';
class Notes extends StatefulWidget {
createState() => _Notes();
}
class _Notes extends State<Notes> {
var title;
var description;
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Consumer<ProviderData>(builder:
(BuildContext context, ProviderData value, Widget? child) {
return ListView.builder(
itemCount: value.newusers.length,
itemBuilder: ((context, index) {
return ListTile(
title: Text(value.newusers[index].title!),
leading: Text(value.newusers[index].description!),
);
}));
}),
Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
FloatingActionButton(
backgroundColor: Colors.black54,
child: Center(
child: Icon(
Icons.add,
size: 40,
),
),
onPressed: () => showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text('NOTES INFO'),
content: Column(
children: [
TextFormField(
decoration: InputDecoration(
hintText: 'Enter notes title',
border: OutlineInputBorder()),
onChanged: (value) {
setState(() {
title = value;
});
},
),
SizedBox(height: 50),
TextFormField(
decoration: InputDecoration(
hintText: 'Enter notes description',
border: OutlineInputBorder()),
onChanged: (value) {
setState(() {
description = value;
});
},
),
],
),
actions: <Widget>[
TextButton(
onPressed: () {
setState(() {
GettingNotes().newtitle = title;
GettingNotes().description = description;
});
Navigator.pop(context);
},
child: Text('Save Data'),
),
TextButton(onPressed: () {}, child: Text('Cancel'))
])),
),
],
),
],
),
);
}
}
如果有任何代码错误,请告诉我。我通过使用setState
传递了我的TextForm的值来更改我的标题和描述的当前值,然后期望基于我的标题和描述生成一个列表。
英文:
I am building a note app with details of title and description whereby my input comes from a textfield from an alertDialog popup. Unfortunately, the screen is blank.
This is my UserNotes
class UserNotes{
String? title;
String? descriptioin;
UserNotes({required this.title,required this.descriptioin});
}
This is my provider
import 'package:notes_update/models/UserMethods.dart';
import 'package:notes_update/models/UserNotes.dart';
import 'package:flutter/foundation.dart';
import 'package:provider/provider.dart';
class ProviderData extends ChangeNotifier{
List<UserNotes>_users=[];
List<UserNotes> get newusers=>_users;
IncomingData(){
_users=GettingNotes().addDetails();
notifyListeners();
}
}
This class returns a list of userNotes
import 'UserNotes.dart';
class GettingNotes{
String? newtitle;
String? description;
List<UserNotes> addDetails(){
List<UserNotes>details=[];
if(details.isEmpty){
details.add(UserNotes(title:newtitle!,descriptioin:description! ));
}
return details;
}}
This is my UI
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:notes_update/models/UserMethods.dart';
import 'package:notes_update/models/UserNotes.dart';
import 'package:provider/provider.dart';
import '../Provider/Provder.dart';
class Notes extends StatefulWidget {
createState() => _Notes();
}
class _Notes extends State<Notes> {
var title;
var description;
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Consumer<ProviderData>(builder:
(BuildContext context, ProviderData value, Widget? child) {
return ListView.builder(
itemCount: value.newusers.length,
itemBuilder: ((context, index) {
return ListTile(
title: Text(value.newusers[index].title!),
leading: Text(value.newusers[index].descriptioin!),
);
}));
}),
Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
FloatingActionButton(
backgroundColor: Colors.black54,
child: Center(
child: Icon(
Icons.add,
size: 40,
)),
onPressed: () => showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text('NOTES INFO'),
content: Column(
children: [
TextFormField(
decoration: InputDecoration(
hintText: 'Enter notes title',
border: OutlineInputBorder()),
onChanged: (value) {
setState(() {
description = value;
});
},
),
SizedBox(height:50),
TextFormField(
decoration: InputDecoration(
hintText: 'Enter notes description',
border: OutlineInputBorder()),
onChanged: (value) {
setState(() {
description = value;
});
},
),
],
),
actions: <Widget>[
TextButton(
onPressed: () {
setState(() {
GettingNotes().newtitle = title;
GettingNotes().description = description;
});
Navigator.pop(context);
},
child: Text('Save Data'),
),
TextButton(
onPressed: () {}, child: Text('Cancel'))
])),
),
],
),
],
),
);
}
}
If there is any code mistake, let me know.
I passed on the value of my TextForm using setState to change the current value of my title and description and I expected a list based on my title and description.
答案1
得分: 1
-
你在
TextFormField
中都设置了description
的值,但title
的值从未设置。 -
在
onPressed
中,你创建了UserNotes
类的新实例,而不是GettingNotes
类的实例,并将其添加到ProviderData
类中的笔记列表中。
void addNote({
required String title,
required String description,
}) {
_users.add(UserNotes(title: title, description: description));
notifyListeners();
}
更新后的 UI 代码:
onPressed: () => showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text('NOTES INFO'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
decoration: InputDecoration(
hintText: '输入笔记标题',
border: OutlineInputBorder(),
),
onChanged: (value) {
setState(() {
title = value; // 在这里设置标题
});
},
),
SizedBox(height: 20),
TextFormField(
decoration: InputDecoration(
hintText: '输入笔记描述',
border: OutlineInputBorder(),
),
onChanged: (value) {
setState(() {
description = value;
});
},
),
],
),
actions: <Widget>[
TextButton(
onPressed: () {
if (title != null && description != null) {
Provider.of<ProviderData>(context, listen: false)
.newusers
.add(UserNotes(
title: title,
description: description,
));
Navigator.pop(context);
}
},
child: Text('保存数据'),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('取消'),
),
],
),
)
英文:
Two mistakes:
- In both the
TextFormField
you are settingdescription
value andtitle
value is never set. - In
onPressed
you are creating new instance ofUserNotes
class instead ofGettingNotes
class, and adding it to the list of notes inProviderData
class.
And create a addNote
function, where in you add the note
to the list.
Updated Provider Code:
void addNote({
required String title,
required String description,
}) {
_users.add(UserNotes(title: title, descriptioin: description));
notifyListeners();
}
}
Updated UI code:
onPressed: () => showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text('NOTES INFO'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
decoration: InputDecoration(
hintText: 'Enter notes title',
border: OutlineInputBorder(),
),
onChanged: (value) {
setState(() {
title = value; // 👈 set title here
});
},
),
SizedBox(height: 20),
TextFormField(
decoration: InputDecoration(
hintText: 'Enter notes description',
border: OutlineInputBorder(),
),
onChanged: (value) {
setState(() {
description = value;
});
},
),
],
),
actions: <Widget>[
TextButton(
onPressed: () {
if (title != null && description != null) {
Provider.of<ProviderData>(context, listen: false) //👈 call it this way
.newusers
.add(UserNotes(
title: title,
description: description,
)); //
Navigator.pop(context);
}
},
child: Text('Save Data'),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('Cancel'),
),
],
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论