1
resposta

Unhandled Exception: type 'Contact' is not a subtype of type 'String?' - quando clica em create

Apresentando este erro quando clicado em create New Contact

import 'package:bytebank/src/database/app_database.dart';
import 'package:bytebank/src/models/contact.dart';
import 'package:flutter/material.dart';
import '../themes/colors.dart';
import 'contact_form.dart';

class ContactsList extends StatelessWidget {
  ContactsList({Key? key}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: ThemeColors.primaryColor,
        title: const Text('Contacts'),
      ),
      body: FutureBuilder<List<Contact>>(
        future: findAll(),
        builder: (context, snapshot) {
          final List<Contact>? contacts = snapshot.data;
          if (contacts != null) {
            return ListView.builder(
              itemBuilder: (context, index) {
                final Contact contact = contacts[index];
                return _ContactItem(contact);
              },
              itemCount: contacts.length,
            );
          }
          return Container();
        },),
      //ListView.builder(
      //  itemBuilder: (context, index) {
      //    final Contact
      //    contact = contacts[index];
      //    return _ContactItem(contact);
      //  },
      //  itemCount: contacts.length,
      //),
      floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => const ContactForm(),
            ),
          ).then((newContact) => debugPrint(newContact),
          );
        },
        child: const Icon(Icons.add),
        backgroundColor: Colors.green,
      ),
      bottomNavigationBar: BottomAppBar(
          shape: const CircularNotchedRectangle(),
          color: ThemeColors.primaryColor,
          child: Container(height: 50)),
    );
  }
 }

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,),
        ),
      ),
    );
  }
}

Erro

E/flutter ( 1998): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Contact' is not a subtype of type 'String?' E/flutter ( 1998): #0 ContactsList.build.. (package:bytebank/src/screens/contacts_list.dart:48:45) E/flutter ( 1998): #1 _rootRunUnary (dart:async/zone.dart:1436:47) E/flutter ( 1998): #2 _CustomZone.runUnary (dart:async/zone.dart:1335:19) E/flutter ( 1998): E/flutter ( 1998): I/flutter ( 1998): null

Linha 48 ==>>>  ).then((newContact) => debugPrint(newContact),
1 resposta

No debugPrint você deve passar uma String: debugPrint(newContact.toString())