I have a simple code using stream builder but I keep getting missing return. And I have two return in my code as follow:
Expanded(
flex: 1,
child: Column(
children: [
const Text('Temperature:'),
StreamBuilder<List<int>>(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<List<int>> snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else if (snapshot.connectionState ==
ConnectionState.active) {
// geting data from bluetooth
temperature = _dataParser(snapshot.data);
if (temperature != "nan") {
return Text(temperature);
}
}
}),
],
),
),
CodePudding user response:
You're getting error because you have no "default return". You have return for two cases:
snapshot.hasError
snapshot.connectionState == ConnectionState.active
But if both conditions are false - your code will return null.
Adjust your code like this:
Expanded(
flex: 1,
child: Column(
children: [
const Text('Temperature:'),
StreamBuilder<List<int>>(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<List<int>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else if (snapshot.connectionState == ConnectionState.active || snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return const Text('Error');
}
if (snapshot.hasData) {
temperature = _dataParser(snapshot.data);
if (temperature == "nan") {
return Text("Wrong temperature");
}
return Text(temperature);
} else {
return const Text('Empty data');
}
} else {
return Text('State: ${snapshot.connectionState}');
}
}),
],
),
),
It's just sample code, you can adjust it in way that best fits your needs