1
resposta

snapshot.data ERROR -

Quando compilo e tento executar ele da erro Null check operator used on a null value, alguma forma de resolver isso!

class _FeedTransferState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Transaction Feed'), ), body: FutureBuilder<List>( future: findAll(), builder: (context, snapshot){

        switch(snapshot.connectionState){

          case ConnectionState.none:
            break;
          case ConnectionState.waiting:
            return Progress();
            break;
          case ConnectionState.active:
            break;
          case ConnectionState.done:
            final List<Transaction> transactions = snapshot.data; **Aqui ele pede pra colocar o argumento ? indicando que pode ser null!**
            return ListView.builder(
              itemBuilder: (context, index) {
                final Transaction transaction = transactions[index]; **Quando atribui o ?, aqui ele tbm pedi pra colocar que pode rececer null**
                return Card(
                  child: ListTile(
                    leading: Icon(Icons.monetization_on),
                    title: Text(
                      transaction.value.toString(),
                      style: TextStyle(
                        fontSize: 24.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    subtitle: Text(
                      transaction.contact.accountNumber.toString(),
                      style: TextStyle(
                        fontSize: 16.0,
                      ),
                    ),
                  ),
                );
              },
              itemCount: transactions.length,** Quando atribui o ?, aqui ele tbm pedi pra colocar que pode rececer null.**
            );
            break;
        }

        return Text('Unknown error');
      }),
);

} }

1 resposta

Ao setar na final final List transactions tente fazer a seguinte verificação:

final List<Transaction> transactions = snapshot.data ?? [];

Se estiver usando FutureBuilder, tente tipar dessa forma:

FutureBuilder<List<Transaction>>

e no Builder tente tipar o snapshot também

AsyncSnapshot<List<Transaction>>

exemplo:

FutureBuilder<List<Transaction>>(
        future: _webClient.findAll(),
        builder: (context, AsyncSnapshot<List<Transaction>> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              break;
            case ConnectionState.waiting:
              return Progress();
              break;
            case ConnectionState.active:
              break;
            case ConnectionState.done:
              if(snapshot.hasData){
                final List<Transaction> transactions = snapshot.data ?? [];
                if (transactions.isNotEmpty) {
                  return ListView.builder(
                    itemBuilder: (context, index) {
                      final Transaction transaction = transactions[index];
                      return Card()