I created an rest api method like below,
Future<bool> activateAccount(int id, int code) async{
final body = {"code": '$code'};
final response = await client.post(
'$baseUrl/user/account/$id',
body: body
);
if(response.statusCode == 200){
return true;
}else return false;
}
but i can't use this method in if statement like this:
bool a = userApiService.activateAccount(...)
if(a){
...
}
because:
A value of type 'Future<bool>' can't be assigned to a variable of type 'bool'.
in what is problem?
how to change this method to return it boolean depending on the result of the operation?
I would like to include in my raisedButton if statement:
child: RaisedButton(
onPressed: () {
userApiService.activateAccount(sharedPreferences.getInt('newUserId'), int.parse(activeCode));
// sharedPreferences.clear();
},
child: Text("ENTER",
style: TextStyle(color: Colors.white, fontSize: 16.0, fontWeight: FontWeight.bold)),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(40.0)),
color: Colors.red,
)
Returning the bool like this shorter than yours;
Future<bool> activateAccount(int id, int code) async {
final body = {"code": '$code'};
final response = await client.post('$baseUrl/user/account/$id', body: body);
return response.statusCode == 200;
}
And to use return value is you need to use await keyword;
bool a = await userApiService.activateAccount(...)
if(a){
...
}
for using with your button just add async keyword before curly brackets;
onPressed: () async {
bool a = await userApiService.activateAccount(...)
if(a){
...
}
}
Related
I am looking at my code and wondering for 2 hours now without luck so I will ask for help here.
I have a button, when I press it, it displays a random item from the list view. The problem is I also have a check box on the list view with each item. I do not want it to (Shuffle through the items with the checkbox ticked) only to shuffle through the Task in the list view that are unchecked/unticked/are not done.
Here is my code
class TaskData extends ChangeNotifier {
List<Task> _tasks = [
Task(name: "item1"),
Task(name: "item2"),
Task(name: "item3"),
];
UnmodifiableListView<Task> get tasks {
return UnmodifiableListView(_tasks);
}
int get taskCount {
return _tasks.length;
}
// <<Here is the code that shuffles through list
Future<String> rann() async {
return (_tasks.toList()..shuffle()).first.name;
}
void addTask(String newTaskTitle) {
final task = Task(name: newTaskTitle);
_tasks.add(task);
notifyListeners();
}
void updateTask(Task task) {
task.toggleDone();
notifyListeners();
}
In another script I have this one
class Task {
final String name;
bool isDone;
Task({required this.name, this.isDone = false});
void toggleDone() {
isDone = !isDone;
}
}
In another script file I have this code
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 0),
child: FutureBuilder(
future: Provider.of<TaskData>(context).rann(),
builder: (context, snapshot) {
return Align(
alignment: Alignment.center,
child: Text(
"${snapshot.data}",
//softWrap: true,
textAlign: TextAlign.center,
//textWidthBasis: TextWidthBasis.longestLine,
style: TextStyle(
color: Colors.white,
fontSize: 30,
fontWeight: FontWeight.w700),
),
);
},
),
),
In another script I have this one
class TasksList extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Consumer<TaskData>(
builder: (context, taskData, child) {
return ListView.builder(
itemBuilder: (context, index) {
final task = taskData.tasks[index];
return TaskTile(
taskTitle: task.name,
isChecked: task.isDone,
checkboxCallback: (checkboxState) {
taskData.updateTask(task);
},
);
},
itemCount: taskData.taskCount,
);
},
);
}
}
Any help would be appreciated!
Edit : I also forgot to include this part of code
class TaskTile extends StatelessWidget {
final bool isChecked;
final String taskTitle;
final Function(bool?) checkboxCallback;
final VoidCallback longPressCallback;
TaskTile(
{required this.isChecked,
required this.taskTitle,
required this.checkboxCallback,
required this.longPressCallback});
#override
Widget build(BuildContext context) {
return ListTile(
onLongPress: longPressCallback,
title: Text(
taskTitle,
// at the bottom, it sets decoration of text if isChecked is true, if its not its null
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null),
),
trailing: Checkbox(
activeColor: Colors.blue,
value: isChecked,
onChanged: checkboxCallback,
),
);
}
}
updated:
class TaskData extends ChangeNotifier {
List<Task> _undoneTasksShuffled = []
// you don't need anymore the rann method() instead you should iterate over this listView
UnmodifiableListView<Task> get undoneTasksShuffled => UnmodifiableListView<Task>(_undoneTasksShuffled);
#override
void notifyListeners() {
//this updates _undoneTasksShuffled every time you call notifyListeners
_undoneTasksShuffled = _tasks.where((e)=> !e.isDone).toList()..shuffle();
super.notifyListeners();
}
...
}
I think you only need to filter the results before get a random element. you need to modify your rann method for something like
//you don't really need a future method because you don't have async code
String rann() {
final r = Random();
final undoneTasks = _tasks.where((e)=> !e.isDone).toList();
//this is for avoid RangeException on list. you can return any other thing
if(undoneTasks.isEmpty) return '';
// i think that you don't really need to shuffle whole list, you only need a random element
return undoneTasks[r.nextInt(undoneTasks.length)].name;
}
i hope this solves your question
Hi I wanted to remove the Data from my List during onTap but I am unable to do so.
This is the code:
Widget buildUser(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index){
DocumentSnapshot user = snapshot.data.documents[index];
final style = _selectedBusStop.contains(ListClass(user.data['BusstopName'], user.data['location'].latitude.toString(), user.data['location'].longitude.toString()))
? TextStyle(
fontSize: 18,
color: Colors.blueAccent,
fontWeight: FontWeight.bold,
): TextStyle(fontSize: 18);
return ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(
user.data['image']
),
),
title: Text(user.data['BusstopName'], style: style),
subtitle: Text('Operating Hour: ' + user.data['OperatingHour'], style: style),
trailing:
_selectedBusStop.contains((ListClass(user.data['BusstopName'], user.data['location'].latitude.toString(), user.data['location'].longitude.toString()))) ? Icon(Icons.check, color: Colors.blueAccent, size: 26) : null,
onTap: (){
if(_selectedBusStop.contains(ListClass(user.data['BusstopName'], user.data['location'].latitude.toString(), user.data['location'].longitude.toString()))){
setState(() {
_selectedBusStop.removeWhere((val) => val == ListClass(user.data['BusstopName'], user.data['location'].latitude.toString(), user.data['location'].longitude.toString()));
print (_selectedBusStop);
});
}
},
onLongPress: (){
setState(() {
_selectedBusStop.add(ListClass(user.data['BusstopName'], user.data['location'].latitude.toString(), user.data['location'].longitude.toString()));
print(_selectedBusStop);
});
}
);
},
);
}
This is the Class:
class ListClass{
String Bname;
String Blat;
String Blng;
ListClass(this.Bname, this.Blat, this.Blng);
#override
String toString(){
return '{${this.Bname}, ${this.Blat}, ${this.Blng}}';
}
}
Any idea where went wrong? Thank you in advance.
Update
List _selectedBusStop = [];
_selectedBusStop is empty List and upon LongPress it will add data into the List and upon onPress it will remove the data if the data already exist in the List.
#randomstudent the issue is the instance, when you comaparing two values with different instance but even value is same, it returns false,
for example to understand simply.
void main() {
final a = IntTestWrapper(3);
final b = IntTestWrapper(3);
print(a==b);
}
class IntTestWrapper {
IntTestWrapper(this.a);
final int a;
}
Output: false
If you want to compare, compare using equatable
if you change like this
class IntTestWrapper extends Equatable {
IntTestWrapper(this.a);
final int a;
#override
List<Object> get props => [a];
}
then for this
void main() {
final a = IntTestWrapper(3);
final b = IntTestWrapper(3);
print(a==b);
}
Output will be true.
To print you can override toString
void main() {
final a = IntTestWrapper(3);
final b = IntTestWrapper(3);
print(a);
}
class IntTestWrapper extends Equatable {
IntTestWrapper(this.a);
final int a;
#override
List<Object> get props => [a];
#override
String toString() => 'The value of a is :$a';
}
Output: The value of a is :3
I am creating a List in Flutter and displaying it in a Column, When I Run it is just Empty and when I print the list it just prints an Array
I/flutter (24613): []
I am using this code to create the List:-
myFunction() {
return StreamBuilder(
stream:
users.orderBy('timestamp', descending: true).limit(30).snapshots(),
builder: (context, snapshot) {
List<UserList> usersList = [];
snapshot.data.documents.forEach((doc) {
User user = User.fromDocument(doc);
UserList userList = UserList(user);
usersList.add(userList);
});
return Column (children: usersList);
}
),
}
This is My User Class:-
class User {
final String id;
final String username;
final String email;
final String photoUrl;
User({
this.id,
this.username,
this.email,
this.photoUrl,
});
factory User.fromDocument(DocumentSnapshot doc) {
return User(
id: doc.data()['id'],
username: doc.data()['username'],
email: doc.data()['email'],
photoUrl: doc.data()['photoUrl'],
);
}
}
The Code Is Showing No Errors and the Column Is not Displaying, Also When I print The length of the List it Shows it is Zero:-
I/flutter (24613): 0
What Could be The problem ??
I guess we need to tweak some of your code little bit to make the logic working. :)
builder param should be specified with Type otherwise it will be of type dynamic. To be in safer side in this case it will be QuerySnapshot. So,
builder: (context, snapshot) in your code becomes
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot).
Next, there is no need of looping through foreach and instead you can try something like below.
snapshot.data.docs.map((document) { .... }
snapshot.data.documents in your code is not valid way of getting the Firestore Documents. Please refer official doc
And you need to return a widget from builder which you have done correctly. But, by mistake you are passing the List<UserList> to Column which will be expecting List<Widget>
return Column (children: usersList);
Here I can see you are passing usersList which is of type List<UserList>. So you can replace Column with ListView or similar kind of other widget since, Column doesn't support scroll.
So combining all these bits and pieces you will get the below snippet.
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('users')
.orderBy('timestamp', descending: true)
.limit(30)
.snapshots(), // Just for simplicity.
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
//When there is no data returned from the firebase.
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView(
children: snapshot.data.docs.map((document) {
return Text("Title: " + document['username']);
}).toList(),
);
},
);
For simplicity, I have returned the Text widget. You can implement your own UI there.
NOTE : This is the basic working example and you need to fine tune accordingly like using model classes instead of directly accessing based on your requirements.
Your Code
myFunction() {
return StreamBuilder(
stream:
users.orderBy('timestamp', descending: true).limit(30).snapshots(),
builder: (context, snapshot) {
List<UserList> usersList = [];
snapshot.data.documents.forEach((doc) {
User user = User.fromDocument(doc);
UserList userList = UserList(user);
usersList.add(userList);
});
return Column (children: usersList);
}
),
}
It is because you have to await for the json to actually get parse to the dart model. Second thing is forEach method is synchronous it doesn't wait for the async operation to complete, this is the reason why your list is empty.
This SO question has lot of different ways to make a list work asynchronously in flutter.
Column shows data before fetching data, so it shows empty list. For this use setstate according to your state management type ("notifylisteners" in provider) after getting data, so by this the screen will be updated and column also shows the updated list.
I'm not very sure how you're handling the scope of the variable.
Here's my minimal reproducible code which can give you some idea on how to add the items to the list.
class MyPage extends StatefulWidget {
#override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
final List<Widget> _list = [FlutterLogo()];
#override
void initState() {
super.initState();
Timer.periodic(Duration(seconds: 1), (timer) {
if (timer.tick >= 2) timer.cancel();
setState(() => _list.add(FlutterLogo()));
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: _list),
);
}
}
As 'Ashutosh patole' said, 'forEach' method does not wait iteration's complete.
I think that because of this reason, although you made a 'usersList',
there is no data when build widget in 'usersList'.
To fix this, you'd better change from 'forEach' to 'for'.
void main() async {
List<String> data = [ 'a', 'b', 'c'];
List<String> result = [];
data.forEach((data) async {
await Future.delayed(Duration(seconds: 1));
result.add(data);
});
print(result);
await Future.delayed(Duration(seconds: 3));
print(result);
print('-----------------');
result = [];
for (var item in data) {
await Future.delayed(Duration(seconds: 1));
result.add(item);
}
print(result);
await Future.delayed(Duration(seconds: 3));
print(result);
}
In your code, you can change like below.
List<UserList> usersList = [];
for (var doc in snapshot.data.documents) {
User user = User.fromDocument(doc);
UserList userList = UserList(user);
usersList.add(userList);
}
Before calling the data, check all fields:
Firestore Docs
Add a print() to see where the problem
FutureBuilder<DocumentSnapshot>(
future: users.doc(documentId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
//This
if (snapshot.hasError) {
return Text("Something went wrong");
}
print(snapshot.data);
//This
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
print(snapshot.data);
//This
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
return Text("Full Name: ${data['full_name']} ${data['last_name']}");
}
return Text("loading");
},
);
This is what i typically use.Try out this! Please balance the brackets in the code
FutureBuilder(
future: users.orderBy('timestamp', descending: true).limit(30),
builder: (BuildContext context, AsyncSnapshot<List<User>> snapshot) {
List<User>ulist=snapshot.data;
return ListView.builder(
shrinkWrap: true,
padding: EdgeInsets.only(top: 25,bottom: 35),
itemCount: evlist==null?0:evlist.length,
itemBuilder: (BuildContext context, int index) {
String evtime=evlist[index].fromdate.substring(11,16);
String ontime=evlist[index].fromdate.substring(0,16);
return Container(
decoration: BoxDecoration(
border: Border.all(width: 1.8,color: Colors.indigo[900]),
borderRadius: BorderRadius.circular(12.0),
color: Colors.grey[200]
),
margin:
const EdgeInsets.symmetric(horizontal: 18.0, vertical: 4.0),
child: ListTile(
leading: Icon(Icons.notifications),
title: Text(ulist[index].username.toString()),
subtitle:Text("next data"),
),
);
},
);
I have a problem with that situation. Can you help me ? I'm taking this error message.
Exception has occurred.
NoSuchMethodError (NoSuchMethodError: The method 'addItem' was called on null.
Receiver: null
Tried calling: addItem("{\"name\":\"example\",\"isCompleted\":false,\"isArchived\":false}"))
I'm using addItem here;
floatingActionButton: FloatingActionButton(
backgroundColor: Color(0xff655c56),
onPressed: () async {
String itemName = await showDialog(
context: context,
builder: (BuildContext context) => ItemDialog());
if (itemName.isNotEmpty) {
var item =
Item(name: itemName, isCompleted: false, isArchived: false);
_itemService.addItem(item.toJson());
setState(() {});
}
},
And I define addItem here;
Future<List<Item>> fetchItems() async {
final response = await http.get(_serviceUrl);
if (response.statusCode == 200) {
Iterable items = json.decode(response.body);
return items.map((item) => Item.fromJson(item)).toList();
} else {
throw Exception('something went wrong');
}
}
Future<Item> addItem(String itemJson) async{
final response = await http.post(_serviceUrl, headers: {
'content-type':'application/json'
},body: itemJson);
if(response.statusCode==201){
Map item= json.decode(response.body);
return Item.fromJson(item);
}
else {
throw Exception('something went wrong');
}
}
}
HELPPPP!
fluter up to date btw
It seems that you try call addItem method on _itemService that not assigned to anything. But for more clear answer please share all codes relate this issue.
I am trying to list categories of products in my app, and i am using provider package for state management. During the build time its shows the list is empty even though it is not, then I add a click event to update the list that time it works.
I call the getAllCategories() function during the splash screen and the vallCategoryList has values.
This is my category class
class Category with ChangeNotifier{
int catId;
String catName;
String catThumbnail;
List<SetCategory> allCategoryList = [];
void getAllCategories() async {
String categoryUrl = 'https://app.ecwid.com/api/ccccccc';
Response allCategory = await get(categoryUrl);
print('getAllCategories');
if (allCategory.statusCode == 200) {
var categoryData = allCategory.body;
int totalcount = jsonDecode(categoryData)['count'];
if (allCategoryList.length != totalcount) {
allCategoryList.clear();
for (int i = 0; i < totalcount; i++) {
allCategoryList.add(SetCategory(
catId: jsonDecode(categoryData)['items'][i]['id'],
catName: jsonDecode(categoryData)['items'][i]['name'],
catThumbnail: jsonDecode(categoryData)['items'][i]['thumbnailUrl'],
));
}
}
}
print('allcategorylist length ${allCategoryList.length}');
notifyListeners();
}
}
class SetCategory {
int catId;
String catName;
String catThumbnail;
SetCategory(
{ this.catId, this.catName, this.catThumbnail});
}
My code for screen
class HomeScreen extends StatefulWidget {
static const String id = 'homeScreen';
// static int reload = 0;
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
#override
Widget build(BuildContext context) {
final category = Provider.of<Category>(context);
print('category length ${category.allCategoryList.length}'); // here it shows length as 0 even though it has a value of 16.
return Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Category ${category.allCategoryList.length}',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 15.0,
),
textAlign: TextAlign.start,
),
),
InkWell(
onTap: () {
category.getAllCategories(); // when i tap here this updates the list
}),
],
),
Did you use ChangeNotifierProvider in your Widget as shown here
If you just used Provider it is not updating but just makes the object accessible from the descendants
Solved by adding a Consumer, changed code like this
child: Consumer<Category>(
builder: (_,category,__){
return ListView.builder();
}