Getting ExpansionPanelList to work inside Streambuilder in flutter - firebase

I'm trying to arrange some data streamed from Firebase with an ExpansionPanellist. The panelList is placed inside a StreamBuilder, and above the StreamBuilder i have a SingleChildScrollView.
I am able to get the list showing with the headers, but i can't get the expand/collapse function to work, so I am not able to see the body-text.
screenshot of the list
The expanding/collapinsg function worked outside the Streambuilder, but I was not able to access the data from Firebase then.
Any help will be much appreciated! If this is the wrong way of doing this, I will also be grateful for any pointers to alternative ways of achieving this. (There won't be any data added to the server while looking at past climbs and graphs, so a streambuilder might not be necessary if there are easier/better ways).
-Kristian
class Graphs extends StatefulWidget {
static String id = 'graphs_screen';
#override
_GraphsState createState() => _GraphsState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(tabs: [
Tab(text: 'Graphs'),
Tab(text: 'Stats'),
Tab(text: 'Climbs'),
]),
),
body: TabBarView(
children: [
//Image.asset('assets/images/line_graph.png'),
Expanded(child: NumericComboLinePointChart.withSampleData()),
Container(
child: Text(''),
),
SingleChildScrollView(
child: DataStream(),
),
],
)),
),
);
}
}
class DataStream extends StatefulWidget {
#override
_DataStreamState createState() => _DataStreamState();
}
class _DataStreamState extends State<DataStream> {
#override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('climbs')
.orderBy('Date', descending: true)
.snapshots(),
builder: (context, snapshot) {
List<ExpansionItem> expansionList = <ExpansionItem>[];
if (snapshot.hasData) {
final alldata = snapshot.data.docs;
for (var data in alldata) {
final dataFunction = data.data();
final grades = dataFunction['gradeScore'];
final climbDate = dataFunction['Date'];
final climbDateT = DateTime.fromMicrosecondsSinceEpoch(
climbDate.microsecondsSinceEpoch);
String climbDateString =
"${climbDateT.year.toString()}-${climbDateT.month.toString().padLeft(2, '0')}-${climbDateT.day.toString().padLeft(2, '0')} ${climbDateT.hour.toString()}-${climbDateT.minute.toString()}";
final climber = dataFunction['sender'];
final currentUSer = loggedInUser.email;
if (climber == loggedInUser.email) {
expansionList.add(ExpansionItem(
dateTimeHeader: climbDateString,
climbs: grades.toString()));
}
}
}
return ExpansionPanelList(
expansionCallback: (int index, bool isExpanded) {
setState(() {
print('tap registered');
expansionList[index].isExpanded = !isExpanded;
});
},
children: expansionList.map((ExpansionItem item) {
return ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded) {
return Container(
child: Text(item.dateTimeHeader),
);
},
body: Container(
child: Text(item.climbs),
),
isExpanded: item.isExpanded,
);
}).toList(),
);
});
}
}
class ExpansionItem {
ExpansionItem({this.isExpanded: false, this.dateTimeHeader, this.climbs});
bool isExpanded;
final String dateTimeHeader;
final String climbs;
}

I also ran into this issue and managed to develop a "work-around" (sorry in advance, it's a bit messy).
The reason your expansion tiles are not expanding is due to the nature of expansionCallback function. Once you press the expand button it also causes your StreamBuilder to rebuild. Therefore, since you're initializing "expansionList" within the StreamBuilder it will reset "isExpanded" back to false no matter how many times you press it. So your best option is to initialize the expansionList outside of the StreamBuilder and modify it from within. Check below for my solution but I welcome anyone to optimize it and/or share a better one.
class ExpansionItem {
String headerValue;
bool isExpanded;
SplitObject item;
ExpansionItem({this.item, this.headerValue, this.isExpanded = false});
}
class MyExample extends StatefulWidget{
#override
_MyExampleState createState() => _MyExampleState();
}
class _MyExampleState extends State<MyExample> {
//Pick a number as large as you see fit to always be more than necessary.
List<ExpansionItem> expansionItems = List<ExpansionItem>.generate('anyNumber', (int index)=> ExpansionItem(isExpanded: false,));
#override
Widget build(BuildContext context) {
return GestureDetector(
child: Scaffold(
appBar: AppBar(
title: Text('Split',style: Theme.of(context).textTheme.headline3,),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(8),
child: Container(
child: StreamBuilder(
builder: (context, streamData){
if(streamData.hasData){
List<SplitObject> items = streamData.data;
//Save data to Expansion list by iterating through it.
for (var i = 0; i < items.length; i++){
try {
expansionItems[i].item =items[i];
expansionItems[i].headerValue =items[i].itemName;
} catch (e) {
// Catch any range errors after trimming list.
if(e.toString().contains('RangeError')) {
expansionItems.add(ExpansionItem(
item: items[i], headerValue: items[i].itemName));
}
}
}
// Trim list
expansionItems = expansionItems.getRange(0, items.length).toList();
return _buildListPanel(expansionItems);
} else {
return ListTile(
title: Text('No items to split.'),
);
}
},
stream: DatabaseService().splitItemData,
),
),
),
)
);
}
Widget _buildListPanel(List<ExpansionItem> expansionItems){
// print(expansionItems[0].isExpanded);
return ExpansionPanelList(
expansionCallback: (int index, bool isExpanded){
setState(() {
expansionItems[index].isExpanded = !isExpanded;
// print(expansionItems[index].isExpanded);
});
},
children: expansionItems.map<ExpansionPanel>((ExpansionItem item){
return ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded){
print(item.isExpanded);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(),
);
},
body: Container(),
isExpanded: item.isExpanded,
);
}).toList(),
);
}
}

You should create a class where your Expansion List will be, then your Stream builder must call it. Doing it this way Expansion Panel List callback will function just normal.
Look:
class _ExpansionPanelClass extends State<ExpansionPanelClass> {
#override
Widget build(BuildContext context) {
return ExpansionPanelList(
elevation: 3,
expansionCallback: (index, isExpanded) {
setState(() {
widget.product[index]['isExpanded'] = !isExpanded;
});
},
animationDuration: const Duration(milliseconds: 600),
children: widget.product
.map(
(item) => ExpansionPanel(
canTapOnHeader: true,
backgroundColor:
item['isExpanded'] == true ? Colors.cyan[100] : Colors.white,
headerBuilder: (_, isExpanded) => Container(
padding:
const EdgeInsets.symmetric(vertical: 15, horizontal: 30),
child: Text(
item['title'],
style: const TextStyle(fontSize: 20),
)),
body: Container(
padding:
const EdgeInsets.symmetric(vertical: 15, horizontal: 30),
child: Text(item['description']),
),
isExpanded: item['isExpanded'],
),
)
.toList(),
);
}
}
Then from your StreamBuilder:
StreamBuilder<QuerySnapshot>(
stream: dbProducts
.collection(ids[i])
.orderBy('order', descending: false)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Map<String, dynamic>> _items = [];
for (var document in snapshot.data!.docs) {
Map data = Map.from(document.data() as Map);
if (data['hide'] == false) {
Map<String, dynamic> map = {
'id': _items.length,
'title': data['name'],
'description': data['ingredients'],
'isExpanded': false
};
_items.add(map);
}
}
return ExpansionPanelClass(product: _items);
} else {
return const Center(
child: CircularProgressIndicator(
color: Colors.brown,
),
);
}
},
),
That's all.

Related

Refreshes the page when Slider values ​change - Flutter, BloC

I'm new to Flutter, I started making a Slider through bloc and writing data to the fireStore, but I ran into a problem - when I change the value in the Slider, the page refreshes and freezes at the Loading stage, this is profile_screen. How can I fix the problem so that the page is not updated in real time, but only by clicking on the Save button?
profile_screen
double _currentSliderValue = 1;
#override
Widget build(BuildContext context) {
// final ProfileBloc infoBloc = context.read<ProfileBloc>();
ProfileCubit cubit = ProfileCubit.get(context);
return Scaffold(
body: Container(
child: Padding(
padding: const EdgeInsets.all(50),
child: BlocBuilder<ProfileCubit, ProfileState>(
builder: (context, state) {
if (state is ProfileInitial) {
return const Center(
child: Text('No Data'),
);
}
if (state is ProfileLoadingState) {
return CircularProgressIndicator();
}
if (state is ProfileLoadedState) {
return FutureBuilder<DocumentSnapshot>(
future: cubit.getInfo(),
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return _userNoInfo(cubit);
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return _userInfo(data, cubit);
}
return Text('Loading');
},
);
}
if (state is ProfileErrorState) {
return Center(
child: Text(state.error.toString()),
);
}
return Container();
})),
));
}
Widget _userInfo(data, ProfileCubit cubit) {
return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Form(
key: _formKeyTwo,
child: Column(children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: MediaQuery.of(context).size.width * 0.2,
child: Slider(
value: _currentSliderValue,
max: 100,
divisions: 100,
label: _currentSliderValue.toString(),
onChanged: (value) {
setState(() {
_currentSliderValue = value;
});
}),
),
SizedBox(width: 100),
_cardData(),
],
),
const SizedBox(height: 50.0),
Center(
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.2,
child: ElevatedButton(
onPressed: () {
cubit.addAndUpdateInfo(
_bisNameContr.text,
_bisAddressContr.text,
_contactNameContr.text,
_contactEmailContr.text,
_phoneNumberContr.text,
_progNameContr.text,
_currentSliderValue.toString());
profile_cubit
class ProfileCubit extends Cubit<ProfileState> {
final Database _firestoreRepo;
ProfileCubit(this._firestoreRepo) : super(ProfileInitial()) {
getInfo();
}
static ProfileCubit get(context) => BlocProvider.of(context);
Future<DocumentSnapshot<Object?>> getInfo() {
try {
emit(ProfileLoadingState());
final Future<DocumentSnapshot<Object?>> infoData =
_firestoreRepo.getData();
emit(ProfileLoadedState(infoData));
return infoData;
} catch (e) {
emit(ProfileErrorState(e.toString()));
throw Exception(e);
}
}
Future<void> addAndUpdateInfo(
final String bisName,
final String bisAddress,
final String contactName,
final String contactEmail,
final String phoneNumber,
final String progName,
final String progYears,
) async {
await _firestoreRepo.addAndUpdateInfo(bisName, bisAddress, contactName,
contactEmail, phoneNumber, progName, progYears);
}
}
cubit_state
abstract class ProfileState extends Equatable {
const ProfileState();
#override
List<Object> get props => [];
}
class ProfileInitial extends ProfileState {}
class ProfileLoadingState extends ProfileState {}
class ProfileLoadedState extends ProfileState {
final Future<DocumentSnapshot<Object?>> dataInfo;
const ProfileLoadedState(this.dataInfo);
}
class ProfileErrorState extends ProfileState {
final String error;
const ProfileErrorState(this.error);
#override
List<Object> get props => [error];
}
If you don't want to build everytime, you can use buildWhen which is available inside BlocBuilder. if the value is true, it will rebuild everytime and if it's false, it will never rebuild. So you can keep a condition over there based on your requirements.

Flutter : Autorun a function whenever a screen is opened

I want to run a function to check Firestore data everytime a screen is loaded.
Here is my code :
class PlaceTile extends StatefulWidget {
//String flag='inactive';
final Place place;
PlaceTile({ this.place });
#override
_PlaceTileState createState() => _PlaceTileState(place);
}
class _PlaceTileState extends State<PlaceTile> {
final Place place;
_PlaceTileState(this.place);
String flag = 'inactive';
void getUserById(String id) {
DatabaseService().placesCollection.document(id).get().then((DocumentSnapshot doc) {
print(doc.data);
});
}
checkUserStatus() async {
final FirebaseAuth auth = FirebaseAuth.instance;
final FirebaseUser user = await auth.currentUser();
String uid = user.uid;
UserDatabaseService().userCollection.getDocuments().then((QuerySnapshot snapshot) {
snapshot.documents.forEach((DocumentSnapshot doc) {
if(doc.documentID == uid)
{
if(doc.data['status']=='true')
{
setState(() {
flag = 'active';
});
}
else
{
setState(() {
flag = 'inactive';
});
}
}
});
});
return flag;
}
void showQueueDetailsPanel() {
showModalBottomSheet(context: context, builder: (context) {
return Container(
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 60.0),
child: QueueDetails(value: place.name),
);
});
}
String value;
#override
initState() {
super.initState();
checkUserStatus();
}
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: 8.0),
child: Card(
margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
child: ListTile(
isThreeLine: true,
leading: CircleAvatar(
radius: 25.0,
backgroundColor: Colors.white,
),
title: Text(place.name),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
//Text("People "+place.totalPeople.toString()),
Text(""),
Text("Token "+place.tokenAvailable.toString()),
],
),
trailing: FlatButton.icon(
icon: Icon(Icons.add_box_rounded),
label: Text('Join'),
onPressed: checkUserStatus() =='inactive' ? showQueueDetailsPanel : null,
),
),
)
);
}
}
The code in its current state doesn't perform the way I expect. It s a fallacy actually. Once flag is set to active, the Join buttons get disabled and for them to get re-enabled they need to be pressed for condition to be checked. But that's not possible since the buttons can't be pressed as they are disabled.
I want to run checkUserStatus() everytime this page with PlaceTile is loaded. How can I achieve that? A code snippet would be helpful.
Add your function to initState:
#override
void initState() {
super.initState();
checkUserStatus();
}
Widget build(BuildContext context) {
return FutureBuilder(
future: futurePackageListModel,
builder: (BuildContext context,
AsyncSnapshot<List<Model>> snapshot) {
if (snapshot.hasData) {
return YourWidget();
} else if (snapshot.hasError) {
return ApiStatusFail();
} else {
return Center(child: CircularProgressIndicator());
}
},
);
}

Search database (SQLite) using a textform in Flutter

I am trying to search my sqlite database, right now it returns all the members, even when text is input to the text form. I have a ListView builder in the memberList constructor that creates cards for each member. What I want it to do is display just the cards that match the users input.
i.e. if a user inputs J it would show only the members that either have first or last name with the letters J.
I can see the query is working properly as I have it printing the count in the dbHelper class and it updates each time I make a change to the textform's text. What I need it to do is essentially refresh the body of the Scaffold onChange of the textform's text, which is not working.
Any suggestions on how I can do this?
I prefer to have the textform in the appbar if at all possible.
Below is my code:
import 'package:flutter/material.dart';
import 'package:troop_mobile_app/MemberFiles/Member.dart';
import 'package:troop_mobile_app/MemberFiles/MemberList.dart';
import 'package:troop_mobile_app/DatabaseFiles/DBHelper.dart';
Future<List<Member>> search(String search) async {
var dbHelper = DBHelper();
Future<List<Member>> members = dbHelper.searchScouts(search);
return members;
}
class SearchFunction extends StatefulWidget {
#override
_SearchFunctionState createState() => _SearchFunctionState();
}
class _SearchFunctionState extends State<SearchFunction> {
TextEditingController controller = TextEditingController();
String searchText = "";
_searchResults(String text) {
return new FutureBuilder<List<Member>>(
future: search(text),
builder: (context, snapshot) {
if (snapshot.hasData) {
return MemberList(snapshot.data);
}
return Container(
alignment: AlignmentDirectional.center,
child: new CircularProgressIndicator(
strokeWidth: 7,
));
});
}
Widget build(BuildContext context) {
//Page Creation returning the UI Home Page Display
return Scaffold(
//Top 'Menu Bar' (AppBar) Creation
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
padding: EdgeInsets.fromLTRB(
20 /*left*/, 0 /*top*/, 20 /*right*/, 0 /*bottom*/),
),
title: TextField(
//initialValue: 'Search...',
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
//fillColor: Colors.white,
//filled: true,
//border:
//OutlineInputBorder(borderRadius: BorderRadius.circular(12.0)),
labelText: 'Search...',
contentPadding: EdgeInsets.fromLTRB(10, 6, 0, 6),
prefixIcon: Icon(Icons.search),
),
onChanged: (text) async {
_searchResults(text);
searchText = text;
},
controller: controller,
),
),
//End Top 'Menu Bar' Creation
//Main Body Creation
body: Container(
child: new FutureBuilder<List<Member>> (
future: search(searchText),
builder: (context, snapshot) {
if (snapshot.hasData) {
return MemberList(snapshot.data);
}
return Container(
alignment: AlignmentDirectional.center,
child: new CircularProgressIndicator(
strokeWidth: 7,
));
}),
)
//End Main Body Creation
);
}
}
MemberList:
import 'package:flutter/material.dart';
import 'MemberCards.dart';
import 'package:troop_mobile_app/MemberFiles/Member.dart';
class MemberList extends StatelessWidget {
final List<Member> members;
MemberList(this.members);
#override
Widget build(BuildContext context) {
return _buildList(context);
}
ListView _buildList(context) {
return ListView.builder(
itemCount: members.length,
itemBuilder: (context, int) {
return MemberCards(members[int], );
},
);
}
}
DBHelper:
Future<List<Map<String, dynamic>>> searchScoutsMap(String search) async {
Database db = await this.database;
print("This works? $db");
var result = await db.rawQuery("SELECT * FROM $memberTable WHERE adult = 'N' AND ($colFirstName Like '%$search%' OR $colLastName Like '%$search%') ORDER BY $colFirstName ASC, $colLastName ASC");
print("result is working? $result");
print(result.length);
return result;
}
Future<List<Member>> searchScouts(String search) async {
var searchResults = await searchScoutsMap(search); // Get 'Map List' from database
print(searchResults.length);
print(searchResults.toString());
int count = searchResults.length; // Count the number of map entries in db table
List<Member> memberList = List<Member>();
// For loop to create a 'Member List' from a 'Map List'
for (int i = 0; i < count; i++) {
print("for loop working: ${i+1}");
memberList.add(Member.fromMapObject(searchResults[i]));
}
print("completed for loop");
return memberList;
}
I was able to solve my mistake after hours of frustrating work...
Here is was my fix:
In the first code snippet I was missing the setState()
I had to wrap the return new FutureBuilder... with setState()
_searchResults(String text) {
return new FutureBuilder<List<Member>>(
future: search(text),
builder: (context, snapshot) {
if (snapshot.hasData) {
return MemberList(snapshot.data);
}
return Container(
alignment: AlignmentDirectional.center,
child: new CircularProgressIndicator(
strokeWidth: 7,
));
});
}
New code snippet shown below:
I hope this helps anyone else out there that runs into a similar issue.
_searchResults(String text) {
setState(() {
return new FutureBuilder<List<Member>>(
future: search(text),
builder: (context, snapshot) {
if (snapshot.hasData) {
return MemberList(snapshot.data);
}
return Container(
alignment: AlignmentDirectional.center,
child: new CircularProgressIndicator(
strokeWidth: 7,
));
});
});
}

Dependencies between models in flutter

Help me please. There is a database that contains several tables:
1) The names of the authors of poems.
2) The names of poems.
One table is linked to another using an ID (in the table with the names of the poems there is a column with the author ID).
It is necessary that by clicking on the author poems of this author open. I can’t figure out how to make this connection.
Authors.dart (The models of authors)
Authors authorsFromJson(String str) {
final jsonData = json.decode(str);
return Authors.fromMap(jsonData);
}
String authorsToJson(Authors data) {
final dyn = data.toMap();
return json.encode(dyn);
}
class Authors {
int id;
String name;
int count;
Authors({
this.id,
this.name,
this.count,
});
factory Authors.fromMap(Map<String, dynamic> json) => new Authors(
id: json["c_id"],
name: json["c_title"],
count: json["c_i_count"],
);
Map<String, dynamic> toMap() => {
"c_id": id,
"c_title": name,
"c_i_count": count,
};
}
PoemsTitle.dart (The models of PoemsTitle)
PoemsTitle poemsTitleFromJson(String str) {
final jsonData = json.decode(str);
return PoemsTitle.fromMap(jsonData);
}
String poemsTitleToJson(PoemsTitle data) {
final dyn = data.toMap();
return json.encode(dyn);
}
class PoemsTitle {
int id;
String title;
int authorsId;
PoemsTitle({
this.id,
this.title,
this.authorsId,
});
factory PoemsTitle.fromMap(Map<String, dynamic> json) => new PoemsTitle(
id: json["ii_i_id"],
title: json["ii_i_title"],
authorsId: json["ii_col_int_2"],
);
Map<String, dynamic> toMap() => {
"ii_i_id": id,
"ii_i_title": title,
"ii_col_int_2": authorsId,
};
}
Column c_title from the category table must be associated with column ii_col_int_2 from the items_info table.
Methods from DBProvider:
...Future<List<Authors>> getAllAuthors() async {
final db = await database;
var res = await db.query('category');
List<Authors> list = res.map((c) => Authors.fromMap(c)).toList();
return list;
}
Future<List<PoemsTitle>> getAllPoemsTitle() async {
final db = await database;
var res = await db.query('items_info');
List<PoemsTitle> list = res.map((c) => PoemsTitle.fromMap(c)).toList();
return list;
}
And pages from FutureBuilder and ListView.builder:
This is a widget where poems are displayed after clicking on the corresponding author:
class FavoriteTab extends StatelessWidget {
int authorId;
int count;
FavoriteTab({this.authorId, this.count});
#override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
child: Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Theme.of(context).accentColor,
borderRadius: BorderRadius.all(Radius.circular(30))
),
child: FutureBuilder<List<PoemsTitle>>(
future: DBProvider.db.getAllPoemsTitle(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(
child: CircularProgressIndicator(),
);
case ConnectionState.done:
{
if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
);
} else if (snapshot.hasData) {
return Scrollbar(
child: ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: this.count,
itemBuilder: (context, index) {
PoemsTitle item = snapshot.data[index];
return ListTile(
title: Text(item.title),
leading: Icon(Icons.receipt),
trailing: Icon(Icons.favorite, color: Colors.red, size: 34),
onTap: () {
},
);
},
)
);
}
return Center(
child: Text('No Data')
);
}
default:
return Container();
}
},
)
),
),
);
}
}
And this is the Widget with the authors:
class MainTab extends StatefulWidget {
#override
_MainTabState createState() => _MainTabState();
}
class _MainTabState extends State<MainTab> {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
child: Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Theme.of(context).accentColor,
borderRadius: BorderRadius.all(Radius.circular(30))
),
child: FutureBuilder<List<Authors>>(
future: DBProvider.db.getAllAuthors(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(
child: CircularProgressIndicator(),
);
case ConnectionState.done:
{
if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
);
} else if (snapshot.hasData) {
return Scrollbar(
child: ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
Authors item = snapshot.data[index];
return ListTile(
title: Text(item.name),
leading: Icon(Icons.folder),
trailing: Text(item.count.toString()),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FavoriteTab(authorId: item.id, count: item.count)
)
);
},
);
},
)
);
}
return Center(
child: Text('No Data')
);
}
default:
return Container();
}
},
)
),
);
}
}
I think the problem is on this line, in the FavoriteTab widget:
future: DBProvider.db.getAllPoemsTitle(),
Here you ALWAYS query all poems. The right thing is to create a method like getPoemOfAuthor(int authorID) in DBProvider, and since you pass the authorId when you open your FavoriteTab, in the FutureBuilder you must call:
future: DBProvider.db.getPoemOfAuthor(authorId),
Hope this will help!

How to get use a list from future and use it inside a listView

I'm trying to get a list of all files in a certain directory.
I get the files from a future function called getUserVideos() if inside the function I try to printu the data, I can see the result, but I can't use the data outside the function.
class _mediaUtentiState extends State<mediaUtenti> {
var lightBlue = Color.fromRGBO(0, 197, 205, 1.0);
var _imagesDir;
#override
void initState() {
super.initState();
getUsersVideos();
}
List<String> Names = [
'Abhishek',
'John',
'Robert',
'Shyam',
'Sita',
'Gita',
'Nitish'
];
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: lightBlue,
appBar: new AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(padding: const EdgeInsets.all(8.0), child: Text('Nome')),
Container(
child: CircleAvatar(
backgroundImage: NetworkImage('http://i.pravatar.cc/300'),
),
),
],
),
backgroundColor: purple,
),
body: new Container(
child: new ListView.builder(
reverse: false,
itemBuilder: (_, int index) => EachList(this.Names[index]),
itemCount: this.Names.length,
),
),
);
}
Future<String> getUsersVideos() async {
print('something');
final Directory extDir = await getExternalStorageDirectory();
final String dirPath = '${extDir.path}/Movies/Veople';
final myDir = new Directory(dirPath);
List<FileSystemEntity> _images;
_images = myDir.listSync(recursive: true, followLinks: false);
print(_images.length);
_imagesDir = _images;
}
}
class EachList extends StatelessWidget {
final String name;
EachList(this.name);
#override
Widget build(BuildContext context) {
return new Card(
child: new Container(
padding: EdgeInsets.all(8.0),
child: new Row(
children: <Widget>[
new CircleAvatar(
child: new Text(name[0]),
),
new Padding(padding: EdgeInsets.only(right: 10.0)),
new Text(
name,
style: TextStyle(fontSize: 20.0),
)
],
),
),
);
}
}
for now I just show a list of names, but I want to show a card for each file in the path.
for example, in the function getUserVideos() whe I try to print imagesDir I get the right result [File: '/storage/emulated/0/Movies/Veople/1556217605345.mp4', File: '/storage/emulated/0/Movies/Veople/1556217605345.png', File: '/storage/emulated/0/Movies/Veople/1556217632709.mp4', File:
...]
But I cannot in any way access _imageDir out of that function.
I'm sure that is it possible to solve this problem with few lines, but right now it's 3 hours and I can't get a solution.
Thankyou!
I thought that for sure this would have already been answered, but while there's a lot of questions about FutureBuilder and Lists, none are quite like this or haven't really been answered adequately.
This is how I'd do it:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
Future<List<FileSystemEntity>> _getUsersVideos() async {
print('something');
final Directory extDir = await getExternalStorageDirectory();
final String dirPath = '${extDir.path}/Movies/Veople';
final myDir = new Directory(dirPath);
List<FileSystemEntity> _images = myDir.listSync(recursive: true, followLinks: false);
return _images;
}
class ListFromFuture extends StatefulWidget {
#override
_ListFromFutureState createState() => _ListFromFutureState();
}
class _ListFromFutureState extends State<ListFromFuture> {
Future<List<FileSystemEntity>> future;
#override
void initState() {
super.initState();
future = _getUsersVideos();
}
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
return Container(
alignment: Alignment.center,
child: Text("Loading"),
);
break;
case ConnectionState.done:
if (snapshot.hasError) {
// return whatever you'd do for this case, probably an error
return Container(
alignment: Alignment.center,
child: Text("Error: ${snapshot.error}"),
);
}
var data = snapshot.data;
return new ListView.builder(
reverse: false,
itemBuilder: (_, int index) => EachList(data[index]),
itemCount: data.length,
);
break;
}
},
);
}
}
The important parts of this are that:
future is only set it initState, not the build function. This makes sure that it isn't called each time the widget builds
I handle all of the cases where either there's an error or the future hasn't completed yet.
To be honest though, your example is actually very close to getting it working. All you'd have to do is wrap the line where you set _imagesDir = images in a setState(() => ...) and it should work (assuming the list doesn't return empty). You should also be checking for _imagesDir == null though, otherwise you might get null pointer exceptions.

Resources