I'm using Provider to receive data to a screen and i have a function with not required parameters in my provider class and the data came normally. I have an icon in the appbar to open a bottomsheet, which i gave a parameter to the function so i press a button in the bottomsheet to update the existing data with the new data. I tried many times but it did not work with me because i still learning state management using provider. Can any one tell me how to it programmaticallye?
Here's the code..
import 'package:conditional_builder_null_safety/conditional_builder_null_safety.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:rick_and_morty/pages/character_details.dart';
import 'package:rick_and_morty/pages/widgets/character_item.dart';
import 'package:rick_and_morty/pages/widgets/chip_choice.dart';
import 'package:rick_and_morty/providers/my_provides.dart';
import '../models/users.dart';
class HomeLayout extends StatefulWidget {
const HomeLayout({Key? key}) : super(key: key);
@override
State<HomeLayout> createState() => _HomeLayoutState();
}
class _HomeLayoutState extends State<HomeLayout> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Center(child: Text('Users')),
actions: [
IconButton(
padding: const EdgeInsets.only(right: 25),
onPressed: (){
showModalBottomSheet(
context: context,
builder: (context) =>
Column(
children: [
Container(
alignment: Alignment.center,
color: Colors.black,
width: double.infinity,
height: 50,
child: const Text('Filter Characters',
style:TextStyle(
color:Colors.white
,fontSize: 22),),
),
const ChoiceChipWidget(),
],
)
);
},
icon: const Icon(Icons.filter_list_sharp,size: 40,)),
],
),
body: FutureBuilder(
future: Provider.of<MyProvider>(context).getAllCharacters(),
builder: (context, snapshot){
if(snapshot.hasData){
List<RickModel> users = snapshot.data!;
return GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 2 / 3,
crossAxisSpacing: 1,
mainAxisSpacing: 1,
),
itemCount: users.length,
itemBuilder: (context,index) =>
CharacterItem(
rickModel: users[index],
widget:CharacterDetails(rickModel: users[index]),
)
);
} else{
return Center(child: CircularProgressIndicator());
}
}
)
);
}
}
import 'package:flutter/material.dart';
import '../models/users.dart';
import '../repository/my_repository.dart';
class MyProvider extends ChangeNotifier{
MyProvider(this._myRepository);
final MyRepository _myRepository;
List<RickModel> _allRickCharacters =[];
List<RickModel> get allRickCharacters => _allRickCharacters;
Future<List<RickModel>> getAllCharacters({String? species, String? gender, String? status }) async{
await _myRepository.getAllCharacters(species: species, gender: gender, status: status)
.then((allRickCharacters){
_allRickCharacters = allRickCharacters;
});
return _allRickCharacters;
}
}
any one can help me in this issue?
I think it just missing notifyListeners
. Also you dont need to use await
and .then
at the same time. I will prefer await
Future<List<RickModel>> getAllCharacters({
String? species,
String? gender,
String? status,
}) async {
final allRickCharacters = await _myRepository.getAllCharacters(
species: species, gender: gender, status: status);
_allRickCharacters = allRickCharacters;
notifyListeners(); //this
return _allRickCharacters; // we may can skip return
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.