i am making a mobile app using flutter. And i am using stream builder for this screen. I am not getting the point where i am wrong in the code. Can you please help me in this. I am sharing code and screenshot for this particular row which is causing problem
var timeSelected = 'Click here';
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
'Time Slot:',
style: TextStyle(color: Colors.white),
),
Spacer(),
GestureDetector(
onTap: () {
_asyncInputDialog(context);
//_displayDialog();
},
child: StreamBuilder(stream: cartManager.getTimeSlotSelected,
initialData: timeSelected,
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData){
timeShow(snapshot,);
}
else if (snapshot.hasError) {
return Text(snapshot.error.toString());
}
return Center(
child: Container(
child: Text('Select time slot'),
),
);
},)
),
],
),
This alert dialog will show when i click on the text of row:
_asyncInputDialog(
BuildContext context,
) {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Center(child: Text('Available Time Slot')),
content: TEAlertDialogContent(),
actions: <Widget>[
new FlatButton(
child: new Text('CANCEL'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
When i got the value from showdialog i will store the value in streamcontroller that is present in CartManager.
static StreamController<Timeslot> timeSlotController = BehaviorSubject();
timeSlotSelected(Timeslot time){
timeSlotController.sink.add(time);
}
get getTimeSlotSelected{
return timeSlotController.stream;
}
And we call the above method in stream property of streamcontroller and get the snapshot. This is the method which was called when our snapshot has data:
Widget timeShow(AsyncSnapshot<Timeslot> snapshot ) {
timeSelected = '${snapshot.data.firstTimeSlot}-${snapshot.data.secondTimeSlot}';
timeslotid = snapshot.data.id.toString();
return Text(timeSelected);
}
But i am getting error: type 'BehaviorSubject' is not a subtype of type 'Stream'
Please let me know where i am wrong. I had also shared a screen shot of screen showing this error too.
As your error states, you are trying to pass a type Timeslot to a Stream builder expecting a stream of type String. You must check which one is correct (String or Timeslot) and use the same type on both sides.
Apparently, your problem is in the timeSelected variable. Where is it defined? If this is a String, the Stream builder will infer that your stream is of type String, which is not true. You must set this variable as a Timeslot, since this is your stream type.
Also, you have an error in your code. You have to return a widget to be rendered if snapshot has data. Check the code below:
StreamBuilder(stream: cartManager.getTimeSlotSelected,
initialData: timeSelected,
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData){
return timeShow(snapshot,);
}
else if (snapshot.hasError) {
return Text(snapshot.error.toString());
}
return Center(
child: Container(
child: Text('Select time slot'),
),
);
},)
Related
I try to use the class that I made in another file as a class. but it occures error that 'The return type 'HomePage' isn't a 'Future', as required by the closure's context.'
I used to use the class as a function shape like this when similar situation that I show picture below 'stream builder function'. I don't understand to compare with this(In stream builder, return HomePage is okay.) that why in 'if~ replacesnackBar', HomePage() is not valid.
Could you explain what is the problem in this case? I'll really appreciate about that.. Thanks.
--code below--
Case1: In stream builder function.
return SizedBox.expand(
child: StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot){
if(!snapshot.hasData){
return SignInScreen(providerConfigs: [
EmailProviderConfiguration()
]);
}
return HomePage();
Case2: In 'if~ replacesnackBar' that I said above.
if (pattern != null) ...[
SizedBox(height: 16),
MaterialButton(
color: Colors.green,
child:
Text("Check Pattern", style: TextStyle(color: Colors.white)),
onPressed: () async {
final result = await Navigator.pushNamed(
context,
"/check_pattern",
arguments: pattern,
);
if (result == true) {
context.replaceSnackbar(
content: Text(
"it's correct",
style: TextStyle(color: Colors.green),
),
);
return HomePage();
I'm a beginner in flutter and want to program a app with Bluetooth LE. I use flutter_blue. My App works if i only want to read the value of one characteristic. Now i need to read a second characteristic and that is the problem. I'm using a example from the esp32 dust sensor. The main is similar to the flutter blue example. The step to the next site is work with the following code:
StreamBuilder<List<ScanResult>>(
stream: FlutterBlue.instance.scanResults,
initialData: [],
builder: (c, snapshot) => Column(
children: snapshot.data
.map(
(r) => ScanResultTile(
result: r,
onTap: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) {
r.device.connect();
return SensorPage(device: r.device);
})),
),
That works ok. At the Sensor-Page there is the code for the Page which starts with:
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
appBar: AppBar(
title: Text(' Connect'),
),
body: Container(
// color: Colors.purple,
decoration: BoxDecoration(
image: DecorationImage(
// image: AssetImage("lib/Images/knaus.jpg"),
image: AssetImage("lib/Images/innen.jpg"),
fit: BoxFit.cover,
),
),
child: !isReady
? Center(
child: Text(
"Warte auf Verbindung...",
style: TextStyle(fontSize: 24, color: Colors.red),
),
)
: Container(
child: StreamBuilder<List<int>>(
stream: wt_Ist_stream,
builder: (BuildContext context,
AsyncSnapshot<List<int>> snapshot) {
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
if (snapshot.connectionState ==
ConnectionState.active) {
_DataParser(snapshot.data);
The characteristics i generate with:
List<BluetoothService> services = await widget.device.discoverServices();
services.forEach((service) {
if (service.uuid.toString() == SERVICE_UUID) {
service.characteristics.forEach((characteristic) {
if (characteristic.uuid.toString() == CHARACTERISTIC_UUID_WT_RcvWater) {
characteristic.setNotifyValue(!characteristic.isNotifying);
characteristic1=characteristic;
// characteristic1.setNotifyValue(!characteristic.isNotifying);
wt_Ist_stream = characteristic1.value;
// wt_Soll_stream = service.characteristic.value;
setState(() {
isReady = true;
});
}
if (characteristic.uuid.toString() == CHARACTERISTIC_UUID_WT_Soll) {
characteristic.setNotifyValue(!characteristic.isNotifying);
characteristic2=characteristic;
// characteristic2.setNotifyValue(!characteristic2.isNotifying);
wt_Soll_stream = characteristic2.value;
// characteristic2.value.listen((InputString2)
// wt_Soll_stream = service.characteristic.value;
}
//Updating characteristic to perform write operation.
if (characteristic.uuid.toString() == CHARACTERISTIC_UUID_WT_TxWater) {
characteristic_Write=characteristic;
}
});
}
});
if (!isReady) {
_Pop();
}
}
Both reading-Characteristics are send Data to the app and i get the notification, that new data available [onCharacteristicChanged] uuid: 456e869c-d393-4cec-9f43-cef5382eab72].
but my Dataparser will only start if the value for uuid ...b72(wt_Ist_stream) changed. Then he gets the right string from snapshot.data.
If i changed the Streambuilder-Stream
child: StreamBuilder<List<int>>(
stream: wt_Ist_stream,
to
child: StreamBuilder<List<int>>(
stream: wt_Soll_stream,
my dataparser gets the value from characteristic 2. But how can i change my app that my parser starts automatic if one stream (wt_Ist_Stream or wt_Soll_stream) changed and then gets the right value.
The Streambuilder sends the right data to the dataparser but only the stream, which is called in stream:
How can i changed the code, that my parser starts on a stream-change and gets the right values from snapshot.data?
The dataparser-code:
_DataParser(List<int> dataFromDevice) {
String InputString = utf8.decode(dataFromDevice);
if (InputString.length>6) {.....}
I am trying to load DropDownMenu inside Future builder.In my widget i have a Column. Inside Column I have a few widget :
#override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(),
Divider(),
Container(),
...widget._detailsModel.data.appletActions.map((item) {
.....
...item.appletInputs.map((inputs) {
FutureBuilder(
future: MyToolsProvider()
.getDropDownConfiges(inputs.dataUrl),
builder:
(ctx,AsyncSnapshot<DropDownModel.DropDownConfigToolsModle>snapshot) {
if (!snapshot.hasData ||
snapshot.connectionState ==
ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.hasData &&
snapshot.connectionState ==
ConnectionState.done) {
_dropDown = snapshot.data.data[0];
return DropdownButton<DropDownModel.DataModle>(
hint: Text("Select Item"),
value: _dropDown,
onChanged: (data) {
setState(() {
_dropDown = data;
});
},
items: snapshot.data.data.map((item) {
return DropdownMenuItem<
DropDownModel.DataModle>(
value: item,
child: Row(
children: <Widget>[
Icon(Icons.title),
SizedBox(
width: 10,
),
Text(
item.title,
style: TextStyle(
color: Colors.black),
),
],
),
);
}).toList(),
);
} else {
return Center(
child: Text('failed to load'),
);
}
}),
}
}
]
As you can see i have FutureBuilder inside a loop to show DropdownButton.everything is ok and code works as a charm but my problem is :
onChanged: (data) {
setState(() {
_dropDown = data;
})
every time setState called, future: MyToolsProvider().getDropDownConfiges(inputs.dataUrl), is executed and
_dropDown = snapshot.data.data[0]; again initialized and it get back in a first time .
It is not possible declared MyToolsProvider().getDropDownConfiges(inputs.dataUrl), in initState() method because inputs.dataUrl it is not accessible there.
How can i fixed that?
Updating parent state from within a builder is anti-pattern here. To reduce future errors and conflicts I recommend to wrap the parts that use and update _dropDown variable as a statefull widget.
Afterward the builder is just responsible of selecting correct widget based on future results and separated widget will only update itself based on interactions. Then hopefully many current and potential errors will disappear.
Do one thing, change this
_dropDown = snapshot.data.data[0];
to
_dropDown ??= snapshot.data.data[0];
What this will do is, it will check if _dropDown is null then assign it with value otherwise it won't.
I am trying to fetch document one by one ...i am using below code its work but in console its show me error like
StreamBuilderBaseState>#de08b):
The getter 'documents' was called on null.
Receiver: null
Tried calling: documents
new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("Quiz").where("topice",isEqualTo: widget.topic).where("section",isEqualTo: widget.section).snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot,) {
int length= snapshot.data.documents.length;
if (!snapshot.hasData)
return new Container(child: Text(""),);
return ListView(
children: <Widget>[
Text(widget.scoren.toString()),
Text(snapshot.data.documents[cunter]["Description"]),
Row(
children: <Widget>[
CupertinoButton(child: Text("COMMENTS"), onPressed: null),
RaisedButton(onPressed: (){
setState(() {
cunter++;
});
// Navigator.of(context).push(new MaterialPageRoute(builder: (context)=>new MyApp()));
},child: Text("NEXT"),)
],
)
],
)
You are trying to access the length property of the documents list before you check if the snapshot has any data.
Try inverting those lines, like this
//Check if the snapshot has data
if (!snapshot.hasData) return Container(child: Text(""));
//If you get here it means you have data
int length= snapshot.data.documents.length;
Also, remember that in Dart 2 the new keyworkd is not needed anymore.
I have a Future Builder that builds a ListView.builder, The builder ListTiles are build by another widget that have an ontap function. I have figured out how to get something to run on the final widget by using the back button, but have not been able to figure out how to call something on the original widget on back button. For instance, I need to refresh the top level data when the user clicks back button and not just the data in the secondary widget which is already working.
I hope this makes sense, any help would be great.
UPDATE Here is the code. I am simplifying what I am showing because making a simple example will lose the context. Below you see that I have a FutureBuilder that returns a ListBuilder that returns a new ChatWidget. This is the top level, this Future needs to be refreshed when I click on the back button. However the onTap to trap the callback is in the ChatWidget.
new Expanded(
child: new RefreshIndicator(
child: new FutureBuilder<List<Map>>(
future: chatlist,
builder: (BuildContext context, AsyncSnapshot<List<Map>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none: return new Text('Waiting to start');
case ConnectionState.waiting: return new Text('Loading...');
default:
if (snapshot.hasError) {
return new Text('Error: ${snapshot.error}');
} else {
return new ListView.builder(
itemBuilder: (context, index) {
ChatServerList mychat = new ChatServerList(snapshot.data[index]['msgkey'],snapshot.data[index]['refid'],snapshot.data[index]['referralname'], snapshot.data[index]['oid'],snapshot.data[index]['sendname'],snapshot.data[index]['pid'],snapshot.data[index]['receivename'],snapshot.data[index]['pgrpid'],snapshot.data[index]['prid'],snapshot.data[index]['message'],);
bool isgroup = true;
if (mychat.grpid == 0) {
isgroup = false;
}
return new ChatWidget(mychat: mychat,threaded: threaded, isgroup: isgroup);
},
itemCount: snapshot.data.length,
);
}
}
},
),
onRefresh: _onRefresh
),
)
This is built in the ChatWidget, you notice the _onTap:
new MyListTile(
leading: new CircleAvatar(
child: _chatAvatar(),
//child: !isgroup ? _indMsg() : _grpMsg(), radius: 18.0,
),
//subtitle: new Text(widget.mychat.oname),
title: new Text(widget.mychat.referralname),
trailing: new Text(widget.mychat.oname, textAlign: TextAlign.right,),
//isThreeLine: true,
//onTap: doTap(threaded),
onTap: _onTap,
onLongPress: _doArchive,
),
new MyListTile(
leading: new Text(' '),
title: new Text(submymessage, textAlign: TextAlign.left,
style: new TextStyle(fontSize: 15.0,
color: Colors.grey, fontStyle: FontStyle.italic),),
trailing: _unreadBabdge(),
onTap: _onTap,
onLongPress: _doArchive,
),
That _onTap is below and has the code to handle the back button.
_onTap() async {
ChatDB.instance.updateRead(widget.mychat.msgkey);
if (threaded) {
//TODO
} else {
Route route = new MaterialPageRoute(
settings: new RouteSettings(name: "/ChatServerDivided"),
builder: (BuildContext context) => new ChatServerDivided(mychat: widget.mychat, title: "Chat Messages",),
);
//Navigator.of(context).push(route);
var nav = await Navigator.of(context).push(route);
if(nav==true||nav==null){
unread = ChatDB.instance.getUnread(widget.mychat.msgkey);
}
}
}
So what I am trying to find is if this code can somehow commmunicate up to the originating widget so that I can run the original Future again. I hope this makes more sense and is easier to understand.
Yes you can do that. Couldn't see exactly where to fit it into your code but I'll give you the way to handle this. The navigator calls are all Futures which means you can await them on the calling side. It seems like you're just missing passing a value to the .pop call. Below is an example.
Where you navigate you can await for your result
var navigationResult = await Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => Page2()));
Then you can check the navigationResult with a simple if.
if(navigationResult == 'rerun_future') {
uploadFiles(); // Perform your custom functionality here.
}
The way you pass that information back is that when you do a pop call (to navigate back) you'll pass the value 'rerun_future' in there.
Navigator.of(context).pop('rerun_future')
Additionally if you also want to add this functionality to the back button you should surround your Scaffold with WillPopScope, return false to onWillPop and supply a leading item to the appBar where you perform your custom pop call. Example below from this post
#override
Widget build(BuildContext context) {
return new WillPopScope(
onWillPop: () async => false,
child: new Scaffold(
appBar: new AppBar(
title: new Text("data"),
leading: new IconButton(
icon: new Icon(Icons.ac_unit),
onPressed: () => Navigator.of(context).pop('rerun_future'), //<----- pass value here too
),
),
),
);
}