Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

accountNumber está retornando como ''null'' (nulo)

Ao executar o código, aparece a mensagem no console;

Performing hot restart... 
Syncing files to device ASUS A001D...
Restarted application in 2.313ms.
I/flutter ( 4104): [Contact{id: 0, name: Gaabriel, accountNumber: null}, Contact{id: 3, name: Gabriel, accountNumber: null}, Contact{id: 4, name: Gabriel, accountNumber: null}, Contact{id: 5, name: Gabriel, accountNumber: null}, Contact{id: 6, name: Gabriel, accountNumber: null}, Contact{id: 7, name: Gabriel, accountNumber: null}, Contact{id: 8, name: Gabriel, accountNumber: null}, Contact{id: 9, name: Gabriel, accountNumber: null}]

Já conferi todo o código e não sei por que está mandando null Segue código do app_database.dart:

import 'package:Bytebankapp/models/contact.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';


Future<Database> createDatabase() {
  return getDatabasesPath().then((dbPath) {
    final String path = join(dbPath, 'bytebank.db');
    return openDatabase(path, onCreate: (db, version) {
      db.execute('CREATE TABLE contacts('
          'id INTEGER PRIMARY KEY, '
          'name TEXT, '
          'account_number INTEGER)');
    }, version: 1);
  });
}

Future<int> save(Contact contact) {
  return createDatabase().then((db) {
    final Map<String, dynamic> contactMap = Map();
    contactMap['name'] = contact.name;
    contactMap['account_number'] = contact.accountNumber;
    return db.insert('contacts', contactMap);
  });
}

Future<List<Contact>> findAll() {
  return createDatabase().then((db) {
   return db.query('contacts').then((maps) {
      final List<Contact> contacts = List();
      for (Map<String, dynamic> map in maps) {
        final Contact contact = Contact(
          map['id'],
          map['name'],
          map['accountNumber'],
        );
        contacts.add(contact);
      }
      return contacts;
    });
  });
}

Segue o código do main.dart também:

import 'package:Bytebankapp/database/app_database.dart';
import 'package:Bytebankapp/screens/contact_form.dart';
import 'package:Bytebankapp/screens/contacts_list.dart';
import 'package:Bytebankapp/screens/dashboard.dart';
import 'package:flutter/material.dart';
import 'package:Bytebankapp/models/contact.dart';

void main() {
  runApp(BytebankApp());
  save(Contact(0, 'Gabriel', 1000)).then((id){
    findAll().then((contacts) => debugPrint(contacts.toString()));
  });
}

class BytebankApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.green[900],
        accentColor: Colors.blueAccent[700],
        buttonTheme: ButtonThemeData(
          buttonColor: Colors.blueAccent[700],
          textTheme: ButtonTextTheme.primary,
        ),
      ),
      home: Dashboard(),
    );
  }
}
2 respostas
solução!

Bom dia Gabriel! Tudo certo irmão? Já achei o problema: é bem aqui ó:

Future<List<Contact>> findAll() {
  return createDatabase().then((db) {
   return db.query('contacts').then((maps) {
      final List<Contact> contacts = List();
      for (Map<String, dynamic> map in maps) {
        final Contact contact = Contact(
          map['id'],
          map['name'],
          map['accountNumber'],
        );
        contacts.add(contact);
      }
      return contacts;
    });
  });
}

O seu map['accountNumber'], está procurando no mapa, e no mapa o nome correto é map['account_number'],

Acredito que vá funcionar :3

Show de bola! Obrigadoo