1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter/Dart: Function return type for returning a ShowDialog based on AsyncValue...

Discussão em 'Mobile' iniciado por Stack, Outubro 1, 2024 às 02:23.

  1. Stack

    Stack Membro Participativo

    I want to create a function for my app that goes through an Async List to check if an item was previously bought, and show a dialog box if it has been. Since Asyncvalue.when() has data, error and loading parameters, Future Type of showDialog creates return type conflict with CircularProgressIndicator or a Error Text message I could possibly show. Can you let me know if his approach below is faulty, or any suggestions?

    Future<Void> itemPreviouslyBought(BuildContext context, Item newItem, AsyncValue<List<Item>> currentItemList) {
    return currentItemList.when(
    data: (item) {
    final matchedElement = item.firstWhere(
    (element) =>
    element.productName.toLowerCase() ==
    newItem.productName.toLowerCase(),
    );

    return showDialog<void>(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
    return AlertDialog(
    title: const Text('Item Previously Bought!'),
    content: SingleChildScrollView(
    child: ListBody(
    children: <Widget>[
    Text("You have bought this item before."),
    ],
    ),
    ),
    actions: <Widget>[
    TextButton(
    child: const Text('Okay'),
    onPressed: () {
    Navigator.of(context).pop();
    },
    ),
    ],
    );
    },
    );
    },
    loading: () => const Center(child: CircularProgressIndicator()),
    error: (e, st) => Center(child: Text(e.toString())),
    );
    }


    Error message is following Center widget isn't returnable from Future<InvalidType> function.

    Continue reading...

Compartilhe esta Página