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

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.

Related

The argument type ‘Widget’ can’t be assigned to the parameter type ‘String’?

How do I use my custom widget Notes? I unfortunately can't use the full code in the AddNoteScreen.
I got this error when I changed a few things from the class I'm taking. Below I've pasted the instructors code, with my custom widget included. I'll comment below with the other changes I tried that lead me to this error.
Custom widget down to bare bones:
class Notes extends StatelessWidget {
TextEditingController notesController = TextEditingController();
#override
Widget build(BuildContext context) {
return TextField(
controller: notesController,
);
}
}
class AddNoteScreen extends StatefulWidget {
User user;
AddNoteScreen({
required this.user,
});
#override
State<AddNoteScreen> createState() => _AddNoteScreenState();
}
class _AddNoteScreenState extends State<AddNoteScreen> {
TextEditingController titleController = TextEditingController();
TextEditingController notesController = TextEditingController();
bool loading = false;
#override
void initState(){
super.initState(
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor:Color (0xFF162242),
elevation: 0,
),
body: GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
new TextEditingController().clear();
},
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(20),
child: Column(children: [
Text("Title", style: TextStyle(
color: Colors.white,
),
),
SizedBox(
height: 15,
),
Container(
height: 60,
color: Colors.white,
child: TextField(
style: TextStyle(
color: Color(0xFF192A4F),
),
controller: titleController,
),
),
Notes(), // My Custom Widget
SizedBox(height: 50,),
loading ? Center (child: CircularProgressIndicator(),) : Container(
height: 50,
width: MediaQuery.of(context).size.width,
child: ElevatedButton(
onPressed: ()async{
if (
titleController.text == "" || notesController.text == "") // HERE
{
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("All fields are required")));
} else {
setState(() {
loading = true;
});
await FirestoreService().insertNote(titleController.text, notesController.text, widget.user.uid); // HERE
setState(() {
loading = false;
});
Navigator.pop(context);
}
}, child: Text("Add Note"),
),),
]),),
),
),
);
}
}
^ above I changed notesController.text == "" to Notes == "" and then notesController.text to Notes()
class FirestoreService{
FirebaseFirestore firestore = FirebaseFirestore.instance;
Future insertNote(String title, String notes, String userId)async{
try{
await firestore.collection('notes').add({
"title":title,
"notes":notes,
"userId": userId
});
} catch (e) {}
}
}
^ above I changed String to Widget for notes
class NoteModel {
String id;
String title;
String notes;
String userId;
NoteModel({
required this.id,
required this.title,
required this.notes,
required this.userId
});
factory NoteModel.fromJson(DocumentSnapshot snapshot){
return NoteModel(
id: snapshot.id,
title: snapshot['title'],
notes: snapshot['notes'],
userId: snapshot['userId']
);
}
}
^ above I changed String to Widget for notes
class HomeScreen extends StatefulWidget {
#override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final user = FirebaseAuth.instance.currentUser!;
FirebaseFirestore firestore = FirebaseFirestore.instance;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Notes'),
centerTitle: true,
backgroundColor: Color (0xFF162242),
actions: [
TextButton(onPressed: () => FirebaseAuth.instance.signOut(), child: Text("Sign Out", style: TextStyle(color: Colors.white),),),
],
),
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection("notes").where('userId', isEqualTo: user.uid).snapshots(),
builder: (context, AsyncSnapshot snapshot){
if (snapshot.hasData){
if(snapshot.data.docs.length > 0){
return ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context,index) {
NoteModel note = NoteModel.fromJson(snapshot.data.docs[index]);
return Card(
margin: EdgeInsets.only(top: 16, left: 10, right: 10, bottom: 16),
child: Column(
children: [
ListTile(
title: Center(child: Text(note.title, style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
),
),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => EditNoteScreen(),));},
),
ListTile(title: Center(child:
Container(
height: 300,
child:
Text(note.notes),),), // HERE
),
]),
);
}
);
}else Center(child: Text("No notes available", style: TextStyle(color: Colors.white),),);
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
],
),
);
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => AddNoteScreen(user: user)));
},
backgroundColor: Color (0xFF162242),
child: Icon(Icons.add),
),
);
}
}
^ Text(note.notes) is where I get the error.
I don't really know what I'm doing but can something like this work ? Totally different answer is okay too!
I'm sorry that's a lot of code. Any help is appreciated.
Also link to the class if anyone is interested https://skl.sh/3wxeMVF
Assumptions
Based on the code and comments I guess the actual class NoteModel and Notes are looking something like this:
class NoteModel {
Notes notes;
...
}
class Notes extends StatelessWidget {
TextEditingController notesController = TextEditingController();
...
}
Problem
This explains the error message The argument type ‘Widget’ can’t be assigned to the parameter type ‘String’?:
Text(note.notes) expects note.notes to be a String. Whereas you changed note.notes to be the Widget Notes.
Solution 1
The widget Text() expects Strings, not another Widget. Thus,
change notes back to a String:
class NoteModel {
String notes;
...
}
Build the rest of your code around this NoteModel, do not change it.
Solution 2
If you want to use
class NoteModel {
Notes notes;
...
}
then the Text widget would be called something like this:
Text(note.notes.notesController.text)
However, this is NOT recommended, as a NoteModel is a data model. And data models should never hold Widgets. A Widget is meant for showing data, not for holding it. A data model and a Widget serve different functions. Keep them separated.
Firebase
Note, that one cannot store whole Widgets (like Notes) in in Firebase but only Strings, Numbers etc.
(Please always post your current code, not code that is indirectly related related to the issue. Otherwise, people will find it very difficult to spot the problem.)

Getting ExpansionPanelList to work inside Streambuilder in flutter

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.

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,
));
});
});
}

How to pass on data from inside a function to different class in flutter

As a Flutter beginner I'm trying to build a calendar app in which I want to fetch data from a specific day from the Firestore database. Right now there's an _onDaySelected function which prints the selected day from the calendar in the flutter console like this 'flutter: 2019-11-04'. I would like this String to be passed on to the MessagesStream class so I can call .document('$currentDay') instead of hardcoding the day like this: .document('2019-11-04'). I have included the code below.
Would anyone know how to do this? Any help would be appreciated!
class RoosterTest extends StatefulWidget {
#override
_RoosterTestState createState() => _RoosterTestState();
}
class _RoosterTestState extends State<RoosterTest>
with TickerProviderStateMixin {
Map<DateTime, List> _events;
List _selectedEvents;
AnimationController _animationController;
CalendarController _calendarController;
final messageTextController = TextEditingController();
String messageText;
static final now = DateTime.now();
static final formatter = DateFormat('yyyy-MM-dd');
static final formatted = formatter.format(now);
#override
void initState() {
super.initState();
final _selectedDay = DateTime.now();
_events = {};
_selectedEvents = _events[_selectedDay] ?? [];
_calendarController = CalendarController();
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 400),
);
_animationController.forward();
}
#override
void dispose() {
_animationController.dispose();
_calendarController.dispose();
super.dispose();
}
void _onDaySelected(DateTime day, List events) {
String currentDay = formatter.format(day).toString();
print('$currentDay');
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
_buildTableCalendar(),
const SizedBox(
height: 8.0,
),
MessagesStream(),
],
),
);
}
Widget _buildTableCalendar() {
return TableCalendar(
calendarController: _calendarController,
events: _events,
startingDayOfWeek: StartingDayOfWeek.monday,
calendarStyle: CalendarStyle(
selectedColor: Colors.red,
todayColor: Colors.blue,
markersColor: Colors.green,
outsideDaysVisible: false,
weekendStyle: TextStyle().copyWith(color: Colors.red),
),
headerStyle: HeaderStyle(
formatButtonTextStyle:
TextStyle().copyWith(color: Colors.white, fontSize: 15.0),
formatButtonDecoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(16.0),
),
),
onDaySelected: _onDaySelected,
onVisibleDaysChanged: _onVisibleDaysChanged,
);
}
class MessagesStream extends StatelessWidget {
#override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('days')
.document('2019-11-04')
.collection('hours')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
);
return _buildList(context, snapshot.data.documents);
},
);
}
You can store the result of the _onDaySelected method in a variable on your state class:
class _RoosterTestState extends State<RoosterTest>
with TickerProviderStateMixin {
String stringOfSelectedDay='defaultValue';
[...]
Don't forget in your first time opening the app you should provide stringOfSelectedDay with a default value. What should your MessagesStream return when the _onDaySelected hasn't been called yet? Maybe today's date?
So, on the method you can store it like:
void _onDaySelected(DateTime day, List events) {
String currentDay = formatter.format(day).toString();
setState(() {
stringOfSelectedDay = currentDay;
});
print('$currentDay');
}
You must use setState to make the MessagesStream rebuild itself after the day has changed.
After that, you must make your MessagesStream class have a final variable to make it build the stream based on it, and a constructor that gives it its value:
class MessagesStream extends StatelessWidget {
final String date;
MessagesStream(this.date); //Constructor
#override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('days')
.document(date) // Using the final variable here
.collection('hours')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
);
return _buildList(context, snapshot.data.documents);
},
);
}
Since you used setState this Widget will reflect different calls of the _onDaySelected method.
Lastly, when you call your Widget to be constructed, pass the variable accordingly:
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
_buildTableCalendar(),
const SizedBox(
height: 8.0,
),
MessagesStream(stringOfSelectedDay), //Using the new constructor you've made.
],
),
);
}

Using TextField inside a Streambuilder

How do we add a TextField inside a StreamBuilder?
I have a TextField / TextFormField as one of the widgets inside the builder function of either a StreamBuilder or FutureBuilder, whenever we try to interact with the textfield it just refreshes the entire builder widget and calls the stream/future again.
body: StreamBuilder(
stream: getClientProfile().snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
print(snapshot.data.data);
Client tempClient = Client.from(snapshot.data);
print('details = ${tempClient.representative.email} ${tempClient
.address.location} ${tempClient.businessDescription}');
return Container(
child: Column(
children: <Widget>[
TextFormField(
)
],
),
);
} else if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else {
return Center(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.error),
),
Text('Error loading data')
],
),
);
}
}),
and firestore function
DocumentReference getClientProfile() {
return _firestore.collection(SELLERS_COLLECTION).document(_uid);
}
What I want to achieve, is to have a form with pre-filled data from firestore document, basically an edit form. Is there any other way I could achieve the same or am I doing something wrong structurally ?
EDIT:
code after suggested edits.
import 'package:flutter/material.dart';
import 'Utils/globalStore.dart';
import 'models/client_model.dart';
import 'dart:async';
class EditProfileInformation extends StatefulWidget {
#override
EditProfileInformationState createState() {
return new EditProfileInformationState();
}
}
class EditProfileInformationState extends State<EditProfileInformation> {
Stream dbCall;
final myController = TextEditingController();
#override
void initState() {
// TODO: implement initState
super.initState();
dbCall = getClientProfile().snapshots();
myController.addListener(_printLatestValue);
}
_printLatestValue() {
print("Second text field: ${myController.text}");
}
#override
void dispose() {
myController.removeListener(_printLatestValue);
myController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
// key: _scaffoldKey,
appBar: AppBar(
title: Text(
'Edit profile',
style: TextStyle(),
),
),
body: StreamBuilder(
stream: dbCall,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
print(snapshot.data.data);
Client tempClient = Client.from(snapshot.data);
print('details = ${tempClient.representative.email} ${tempClient
.address.location} ${tempClient.businessDescription}');
return Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: myController,
),
)
],
),
);
} else if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else {
return Center(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.error),
),
Text('Error loading data')
],
),
);
}
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
},
child: Icon(Icons.done),
),
);
}
}
In order to use a StreamBuilder correctly you must ensure that the stream you are using is cached on a State object. While StreamBuilder can correctly handle getting new events from a stream, receiving an entirely new Stream will force it to completely rebuild. In your case, getClientProfile().snapshots() will create an entirely new Stream when it is called, destroying all of the state of your text fields.
class Example extends StatefulWidget {
#override
State createState() => new ExampleState();
}
class ExampleState extends State<Example> {
Stream<SomeType> _stream;
#override
void initState() {
// Only create the stream once
_stream = _firestore.collection(collection).document(id);
super.initState();
}
#override
Widget build(BuildContext context) {
return new StreamBuilder(
stream: _stream,
builder: (context, snapshot) {
...
},
);
}
}
EDIT: it sounds like there are other problems which I cannot diagnose from the code snippet you provided.

Resources