1
resposta

Erro hot restart the getter "length" was called on null

Na hora de dar o hot restart ele me aparece uma janela de erro na run dizendo isso dai:

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building FutureBuilder<List<Contact>>(dirty, state: _FutureBuilderState<List<Contact>>#9250f):
The getter 'length' was called on null.
Receiver: null
Tried calling: length

The relevant error-causing widget was: 
  FutureBuilder<List<Contact>> file:///C:/Users/vinic/StudioProjects/bytebankapp/lib/telas/listacontatos.dart:14:13
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      ContactsList.build.<anonymous closure> (package:bytebankapp/telas/listacontatos.dart:23:33)
#2      _FutureBuilderState.build (package:flutter/src/widgets/async.dart:775:55)
#3      StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
...
====================================================================================================

Segue o meu código da lista de contatos:

import 'package:bytebankapp/database/app_database.dart';
import 'package:bytebankapp/models/contacts.dart';
import 'package:bytebankapp/telas/contactform.dart';
import 'package:flutter/material.dart';

class ContactsList extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Contacts"),
      ),
      body: FutureBuilder(
        future: findAll(),
        builder: (context, snapshot) {
          final List<Contact> contacts = snapshot.data as List<Contact>;
          return ListView.builder(
            itemBuilder: (context, index) {
              final Contact contact = contacts[index];
              return _ContactItem(contact);
            },
            itemCount: contacts.length,
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context)
              .push(
                MaterialPageRoute(
                  builder: (context) => ContactForm(),
                ),
              )
              .then(
                (newContact) => debugPrint(newContact.toString()),
              );
        },
        child: Icon(
          Icons.add,
        ),
      ),
    );
  }
}

class _ContactItem extends StatelessWidget {
  final Contact contact;

  _ContactItem(this.contact);

  @override
  Widget build(BuildContext context) {
    return Card(
      child: ListTile(
        title: Text(
          contact.name,
          style: TextStyle(fontSize: 24.0),
        ),
        subtitle: Text(
          contact.accountNumber.toString(),
          style: TextStyle(fontSize: 16.0),
        ),
      ),
    );
  }
}
1 resposta

Esse erro significa que sua lista esta nula, voce esta tentando colocar um item dessa lista em uma variavel pelo index que ainda não existe, voce deve esperar o retorno do metodo findAll() para renderizar o _ContactItem()

Uma solução rapida e facil seria adicionar uma validação antes de utilizar sua lista poor exemplo:

if (!snapshot.hasData) { return Center( child: CircularProgressIndicator(), }else{ final List contacts = snapshot.data as List; return ListView.builder( itemBuilder: (context, index) { final Contact contact = contacts[index]; return _ContactItem(contact); }, }