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

[Flutter] Firestore not accepting int? -> Unhandled Exception: FormatException: Invalid number...

Discussão em 'Mobile' iniciado por Stack, Outubro 21, 2024 às 21:42.

  1. Stack

    Stack Membro Participativo

    In this part of my code, I need to pass a value as int to Cloud Firestore. In the past, this code used to work, but now its not working.

    class AddPatientPage extends StatefulWidget {
    const AddPatientPage({super.key});

    @override
    State<AddPatientPage> createState() => _AddPatientPageState();
    }

    class _AddPatientPageState extends State<AddPatientPage> {
    // ignore: prefer_final_fields
    GlobalKey<FormState> _formKey = GlobalKey<FormState>();
    int id = 0;
    bool isLoading = false;
    @override
    Widget build(BuildContext context) {
    return GestureDetector(
    onTap: () {
    FocusManager.instance.primaryFocus?.unfocus();
    },
    child: Scaffold(
    appBar: AppBar(
    leading: backButton(context),
    title: const Text('CADASTRO', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),),
    backgroundColor: const Color.fromARGB(255, 6, 158, 160),
    actions: [
    TextButton(
    onPressed: () async {
    FocusManager.instance.primaryFocus?.unfocus();
    if (_formKey.currentState!.validate()) {
    setState(() {
    isLoading = true;
    });
    var doc =
    FirebaseFirestore.instance.collection('patients').doc();
    await doc.set({
    'id': id,
    }).whenComplete(() {
    setState(() {
    isLoading = false;
    id = 0;
    _formKey.currentState!.reset();
    });
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
    content: Text('Paciente adicionado'),
    duration: Duration(seconds: 2),
    backgroundColor: Color.fromARGB(255, 6, 158, 160),
    ));
    });
    }
    },
    child: const Text(
    'Adicionar',
    style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
    ))
    ],
    ),
    body: Center(
    child: Padding(
    padding: const EdgeInsets.symmetric(horizontal: 10),
    child: Stack(
    alignment: Alignment.center,
    children: [
    Form(
    key: _formKey,
    child: SingleChildScrollView(

    child: ConstrainedBox(
    constraints: const BoxConstraints(
    maxWidth: 500
    ),
    child: Column(
    children: [
    space(),
    textFormField(
    label: 'ID',
    onChanged: (value) {
    setState(() {
    id = int.parse(value);
    });
    },
    keyboardType: TextInputType.number,
    inputFormatters: [MaskedInputFormatter('####')]),
    ],
    ),
    ),
    ),
    ),
    isLoading == true
    ? Container(
    height: double.infinity,
    width: double.infinity,
    alignment: Alignment.center,
    child: const CircularProgressIndicator())
    : const SizedBox.shrink()
    ],
    ),
    ),
    ),
    ),
    );
    }
    }


    Error message:

    [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Invalid number (at character 1)
    #0 int._handleFormatError (dart:core-patch/integers_patch.dart:125:7) integers_patch.dart:125
    #1 int.parse (dart:core-patch/integers_patch.dart:51:14) integers_patch.dart:51
    #2 _AddPatientPageState.build.<anonymous closure>.<anonymous closure> (package:mutirao24/add%20pages/add_patient_page.dart:154:44)
    add_patient_page.dart:154
    #3 State.setState (package:flutter/src/widgets/framework.dart:1203:30)
    framework.dart:1203
    #4 _AddPatientPageState.build.<anonymous closure> (package:mutirao24/add%20pages/add_patient_page.dart:153:33)
    add_patient_page.dart:153
    #5 _TextFormFieldState.reset (package:flutter/src/material/text_form_field.dart:398:31)
    text_form_field.dart:398
    #6 FormState.reset (package:flutter/src/widgets/form.dart:319:13) form.dart:319
    #7 _AddPatientPageState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>
    (package:mutirao24/add%20pages/add_patient_page.dart:117:48)
    add_patient_page.dart:117
    #8 State.setState (package:flutter/src/widgets/framework.dart:1203:30)
    framework.dart:1203
    #9 _AddPatientPageState.<…>

    Continue reading...

Compartilhe esta Página