Flutter error related with body_might_complete_normally - flutter

Hi I'm practicing flutter.
It's from some open source below. It makes error about body_might_complete_normally.
How can I work around it?
Widget _buildBody(dynamic index) {
switch(index) {
case 0:
return MainPage();
case 1:
return CatalogPage();
case 2:
return NotificationPage();
case 3:
return MorePage();
}
}

If the above case doesn't satisfy, widget will return nothing. You can include default on switch.
Widget _buildBody(int index) {
switch (index) {
case 0:
return MainPage();
case 1:
return CatalogPage();
case 2:
return NotificationPage();
case 3:
return MorePage();
default: //this
return Text("default");
}
// return Text("aa"); or this
}
More about switch-and-case

Related

DART : I have a switch case of enums, which are buttons, but all cases are executing only once. It wont work the second time i click the same button

Below is the switch case code, the main problem I have is, the code is fine for the first time that is appropriate output is rendered, but when I click the button (enum for example Scholarships) the code wont execute
PopupMenuButton(
onSelected: (Category selectedOffer) {
switch (selectedOffer) {
case Category.Scholarships:
{
offersData.showScholarshipsOnly();
}
break;
case Category.Investing:
{
offersData.showInvestingOnly();
}
break;
case Category.Courses:
{
offersData.showCoursesOnly();
}
break;
case Category.Discounts:
{
offersData.showDiscountsOnly();
}
break;
case Category.Finance:
{
offersData.showFinanceOnly();
}
break;
case Category.All:
{
offersData.showAll();
} break;
}
These are the functions which are linked to the above conditions
List<Offers> get offerList {
if (_showScholarships) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Scholarships')).toList();
}
else if (_showCourses) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Courses')).toList();
}
else if (_showInvesting) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Investing')).toList();
}
else if (_showFinance) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Finance')).toList();
}
else if (_showDiscounts) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Discounts')).toList();
}
else if (_showAll) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains(
'Investing,Finance,Discounts,Insurance,Courses,Scholarships'))
.toList();
}
return _offerList;
}

A value of type 'Stream<dynamic>' can't be returned from the function because it has a return type of 'Stream<Color>'

After migrating to flutter 2, the following section of code no longer works:
class ColorBloc extends BlocBase {
// streams of Color
StreamController _streamListController = StreamController<Color>.broadcast();
// sink
Sink get colorSink => _streamListController.sink;
// stream
Stream<Color> get colorStream => _streamListController.stream;
// function to change the color
changeColor(String chosenColour) {
switch(chosenColour) {
case 'blackTheme':
{
colorSink.add(AppState.blackTheme);
}
break;
case 'blueTheme':
{
colorSink.add(AppState.blueTheme);
}
break;
case 'greenTheme':
{
colorSink.add(AppState.greenTheme);
}
break;
case 'redTheme':
{
colorSink.add(AppState.redTheme);
}
break;
case 'whiteTheme':
{
colorSink.add(AppState.whiteTheme);
}
break;
}
}
#override
dispose() {
_streamListController.close();
}
}
The issue being with this line:
Stream<Color> get colorStream => _streamListController.stream;
And the reason given is:
A value of type 'Stream<dynamic>' can't be returned from the function 'colorStream' because it has a return type of 'Stream<Color>'.
But I am not really sure what this means, or how to go about trying to resolve it.
You just need to edit this line StreamController _streamListController = StreamController<Color>.broadcast(); to StreamController<Color> _streamListController = StreamController<Color>.broadcast();
The error says that colorStream is a Stream of Color but you are giving it a Stream of dynamic and dynamic could be anything. So it's a wrong type assignment. Stream of course is generic.
Try adding the Type Color to your StreamController like so:
StreamController<Color> _streamListController = StreamController<Color>.broadcast();

How to await/async with the .length method for an array?

I'm checking for a condition on login so I can route to different screens depending on the condition result. Basically if the array has a length == 0 I want to go to Phone();
Otherwise I want it to go to HomeScreen() The condition is checking an array in Firestorefor length. In my app when the Length is not equal to zero if goes to Phone(); momentarily before jumping to HomeScreen. I suppose my problem is a delay fetching the length in Firestore . I'm not sure how to go about implementing await/async in the condition statement...
case AuthStatus.signedIn: {
print('################################################');
if (userData.services.length==0) {
print('Here I am!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
print(userData.services.length);
return Phone();
} else {
return
HomeScreen();
}
} break;
You can use this, in some cases if you can't use async/ await, you can use then method for it.
case AuthStatus.signedIn: {
print('################################################');
int length;
userData.services.length.then((dataLength){
length = dataLength;
});
if (length==0) {
print('Here I am!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
print(length);
return Phone();
} else {
return
HomeScreen();
}
} break;

Hyperledger Composer REST Server returns 200 even when transaction fails

I'm trying to get Hyperledger Composer REST server to return a 500 error code when a transaction fails, but it will only return a 200.
Here is the transaction:
/**
* Move a player totem
* #param {org.pandemic.board.MoveTotem} txData
* #transaction
*/
function moveTotem(txData) {
let moveType = txData.moveType;
let boardId = txData.boardId;
let totemName = txData.totemName;
let destination = txData.destination;
switch(moveType) {
case "DRIVE_FERRY":
driveFerry(boardId, totemName, destination);
break;
case "DIRECT_FLIGHT":
directFlight(boardId, totemName, destination);
break;
case "CHARTER_FLIGHT":
charterFlight(boardId, totemName, destination);
break;
case "SHUTTLE_FLIGHT":
shuttleFlight(boardId, totemName, destination);
break;
default:
throw new Error("Invalid move type specified");
}
}
And specifically the first case statement:
function driveFerry(boardId, totemName, destination) {
return getAssetRegistry('org.pandemic.board.Board').then((registry) => {
return registry.get(boardId).then((board) => {
let playerIdx = getPlayerTotemIdx(board, totemName);
checkRemainingActions(board, playerIdx);
let currentBoardCity = getCurrentBoardCityForPlayer(board, playerIdx);
if(currentBoardCity == null || currentBoardCity.length == 0) {
throw new Error("Player is not in a city, how did that happen? Ending game...");
//TODO: end the game
}
if(currentBoardCity.connections.indexOf(destination) > -1) {
board.players[playerIdx].currentLocation = destination;
board.players[playerIdx].actionsRemaining -= 1;
return registry.update(board);
} else {
return Promise.reject("Destination is not connected to current city");
//throw new Error();
}
});
});
}
I've tried throw new Error() and (as seen above) return Promise.reject() but the status code back to the front-end is always 200, plus I get back the transactionId/timestamp and the transaction is in the Historian records. I know that the transaction isn't committing, because I check the world state afterwards and the values are what I expect to see (unchanged).
I figured it out! I need to return the function calls from the initial switch statement in the transaction code, like this:
/**
* Move a player totem
* #param {org.pandemic.board.MoveTotem} txData
* #transaction
*/
function moveTotem(txData) {
let moveType = txData.moveType;
let boardId = txData.boardId;
let totemName = txData.totemName;
let destination = txData.destination;
switch(moveType) {
case "DRIVE_FERRY":
return driveFerry(boardId, totemName, destination);
case "DIRECT_FLIGHT":
return directFlight(boardId, totemName, destination);
case "CHARTER_FLIGHT":
return charterFlight(boardId, totemName, destination);
case "SHUTTLE_FLIGHT":
return shuttleFlight(boardId, totemName, destination);
}
}
That way the promise chains are unbroken, and then where I want the error to be thrown, I need to use return Promise.reject(...)

Cancel Or Exit from PromptDialog.Choice Flow in Microsoft Bot Framework

i have a Prompt with 4 options, the last option is user can quit the Prompt,
i want to implement some code so that the bot exit the Prompt
Image
PromptDialog.Choice(context, this.OnOptionSelected, new List<string>() { FlightsOption, HotelsOption, TrainOption, GobackOption }, "Sure..! Tell me what booking would like to make..?", "Not a valid option", 3);
in above image i had implemented quit option on which if user selects quit it goes to Switch case of quit.
i had also tried context.quit but it throws error
private async Task OnOptionSelected(IDialogContext context, IAwaitable<string> result)
{
try
{
string optionSelected = await result;
switch (optionSelected)
{
case FlightsOption:
context.Call(new FlightDialog(), this.ResumeAfterOptionDialog);
break;
case HotelsOption:
context.Call(new HotelsDialog(), this.ResumeAfterOptionDialog);
break;
case TrainOption:
context.Call(new TrainDialog(), this.ResumeAfterOptionDialog);
break;
case GobackOption:
//want some code here to quit the form
break;
}
}
First of all this is not a Form Flow. This is prompt.
Now You can do something like, either you exit the dialog from the stack like this
try
{
string optionSelected = await result;
switch (optionSelected)
{
case FlightsOption:
context.Call(new FlightDialog(), this.ResumeAfterOptionDialog);
break;
case HotelsOption:
context.Call(new HotelsDialog(), this.ResumeAfterOptionDialog);
break;
case TrainOption:
context.Call(new TrainDialog(), this.ResumeAfterOptionDialog);
break;
case GobackOption:
context.Done<object>(null);
break;
}
}
Or,
You can tell something and then wait for other message in the same dialog like this
try
{
string optionSelected = await result;
switch (optionSelected)
{
case FlightsOption:
context.Call(new FlightDialog(), this.ResumeAfterOptionDialog);
break;
case HotelsOption:
context.Call(new HotelsDialog(), this.ResumeAfterOptionDialog);
break;
case TrainOption:
context.Call(new TrainDialog(), this.ResumeAfterOptionDialog);
break;
case GobackOption:
await context.PostAsync("Ok, you came back. Now tell something new.");
context.Wait(MessageReceivedAsync);
break;
}
}
And the next message will go here
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
context.Wait(MessageReceivedAsync);
}