Datepicker problem in Flutter

Asked

Viewed 524 times

1

I have a problem when it comes time to set the state of DatePicker in Flutter.

The same returns me the error:

Another Exception was thrown: Failed assertion: Boolean Expression must not be null

Code:

import 'dart:async';
import 'package:flutter/material.dart';
import 'main.dart';

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPage createState() => _RegisterPage();
}

class _RegisterPage extends State<RegisterPage> {
  DateTime data;
  final _formKey = GlobalKey<FormState>();
  
  Future<Null> _nascimento(BuildContext context) async {
    final DateTime selecionado = await showDatePicker(
      context: context,
      initialDate: data == null ? DateTime.now() : data,
      firstDate: DateTime(1950),
      lastDate: DateTime.now()
    );

    if(selecionado != null && selecionado != data) {
      print('Data:  ${selecionado.toString()}');
   
      setState(() {
        data = selecionado;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            ClipPath(
              clipper: Topo(),
              child: Container(
                width: double.infinity,
                height: 250,
                decoration: BoxDecoration(
                    color: Colors.green,
                    image: DecorationImage(
                        image: AssetImage('assets/pngs/recycle.png'),
                        fit: BoxFit.cover)),
              ),
            ),
            Container(
              width: 160,
              height: 160,
              transform: Matrix4.translationValues(0.0, -140.0, 0.0),
              child: Material(
                elevation: 5.0,
                borderRadius: BorderRadius.circular(100.0),
                // color: Colors.white,
                child: CircleAvatar(
                  backgroundImage: AssetImage('assets/jpgs/no_avatar.jpg'),
                ),
              ),
            ),
            Container(
              transform: Matrix4.translationValues(0.0, -100.0, 0.0),
              child: Padding(
              padding: const EdgeInsets.all(20),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Form(
                    key: _formKey,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: const EdgeInsets.only(top: 10.0, bottom: 10.0),
                          child: Material(
                            borderRadius: BorderRadius.circular(10.0),
                            elevation: 5.0,
                            child: TextFormField(
                              decoration: InputDecoration(
                                prefixIcon: Padding(
                                  padding: EdgeInsets.all(0.0),
                                  child: Icon(
                                    Icons.perm_identity,
                                    color: Colors.green,
                                    size: 26,
                                  ),
                                ),
                                labelText: 'Nome',
                                labelStyle: TextStyle(fontSize: 16),
                                border: OutlineInputBorder(
                                  borderSide: BorderSide(
                                      width: 0, style: BorderStyle.none),
                                  borderRadius: const BorderRadius.all(
                                    const Radius.circular(10.0),
                                  ),
                                ),
                              ),
                              validator: (value) {
                                if (value.isEmpty) {
                                  return 'Please enter some text';
                                }
                              },
                            ),
                          ),
                        ),
                        Container(
                          margin: const EdgeInsets.only(top: 10.0, bottom: 10.0),
                          child: Material(
                            borderRadius: BorderRadius.circular(10.0),
                            elevation: 5.0,
                            child: TextField(
                              onTap: () => _nascimento(context),
                              decoration: InputDecoration(
                                prefixIcon: Padding(
                                  padding: EdgeInsets.all(0.0),
                                  child: Icon(
                                    Icons.date_range,
                                    color: Colors.green,
                                    size: 26,
                                  ),
                                ),
                                labelText: '${data.toString()}',
                                labelStyle: TextStyle(fontSize: 16),
                                border: OutlineInputBorder(
                                  borderSide: BorderSide(
                                      width: 0, style: BorderStyle.none),
                                  borderRadius: const BorderRadius.all(
                                    const Radius.circular(10.0),
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                        Container(
                          margin: const EdgeInsets.only(top: 10.0, bottom: 10.0),
                          child: Material(
                            borderRadius: BorderRadius.circular(10.0),
                            elevation: 5.0,
                            child: TextFormField(
                              decoration: InputDecoration(
                                prefixIcon: Padding(
                                  padding: EdgeInsets.all(0.0),
                                  child: Icon(
                                    Icons.email,
                                    color: Colors.green,
                                    size: 26,
                                  ),
                                ),
                                labelText: 'Email',
                                labelStyle: TextStyle(fontSize: 16),
                                border: OutlineInputBorder(
                                  borderSide: BorderSide(
                                      width: 0, style: BorderStyle.none),
                                  borderRadius: const BorderRadius.all(
                                    const Radius.circular(10.0),
                                  ),
                                ),
                              ),
                              validator: (value) {
                                if (value.isEmpty) {
                                  return 'Please enter some text';
                                }
                              },
                            ),
                          ),
                        ),
                        Container(
                          margin: const EdgeInsets.only(top: 10.0, bottom: 10.0),
                          child: Material(
                            borderRadius: BorderRadius.circular(10.0),
                            elevation: 5.0,
                            child: TextFormField(
                              decoration: InputDecoration(
                                prefixIcon: Padding(
                                  padding: EdgeInsets.all(0.0),
                                  child: Icon(
                                    Icons.lock,
                                    color: Colors.green,
                                    size: 26,
                                  ),
                                ),
                                labelText: 'Senha',
                                labelStyle: TextStyle(fontSize: 16),
                                border: OutlineInputBorder(
                                  borderSide: BorderSide(
                                      width: 0, style: BorderStyle.none),
                                  borderRadius: const BorderRadius.all(
                                    const Radius.circular(10.0),
                                  ),
                                ),
                              ),
                              validator: (value) {
                                if (value.isEmpty) {
                                  return 'Please enter some text';
                                }
                              },
                            ),
                          ),
                        ),
                        Padding(
                            padding: const EdgeInsets.symmetric(vertical: 16.0),
                            child: Container(
                              color: Colors.transparent,
                              width: MediaQuery.of(context).size.width,
                              height: 50,
                              child: FlatButton(
                                shape: new RoundedRectangleBorder(
                                  borderRadius: new BorderRadius.circular(10.0),
                                ),
                                onPressed: () {
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => LoginPage()),
                                  );
                                },
                                color: Colors.green,
                                child: Text(
                                  "Cadastrar",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 16.0,
                                  ),
                                ),
                              ),
                            )),
                      ],
                    ),
                  )
                ],
              ),
            )),
          ],
        ),
      ),
    );
  }
}

class Topo extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var path = Path();

    path.lineTo(0, size.height - 100);
    path.quadraticBezierTo(
        size.width / 2, size.height, size.width, size.height - 100);
    path.lineTo(size.width, 0);
    path.close();

    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return null;
  }
}

After changing the date I get the following error:

I/flutter ( 4615): Date: 2019-07-09 00:00:00.000 I/flutter ( 4615): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 4615): The following assertion was thrown building I/flutter ( 4615): Rawgesturedetector-[Labeledglobalkey#605fc](state: I/flutter ( 4615): Rawgesturedetectorstate#6184a(Gestures: [vertical drag], behavior: Paque)): I/flutter ( 4615): Failed assertion: Boolean Expression must not be null I/flutter ( 4615): I/flutter ( 4615): Either the assertion indicates an error in the framework itself, or we should provide substantially I/flutter ( 4615): more information in this error message to help you determine and fix the underlying cause. I/flutter ( 4615): In either case, Please report this assertion by filing a bug on Github: I/flutter ( 4615): https://github.com/flutter/flutter/issues/new?template=BUG.md I/flutter ( 4615): I/flutter ( 4615): When the Exception was thrown, this was the stack: I/flutter ( 4615): #0 _Rendercustomclip.Clipper= (package:flutter/src/Rendering/proxy_box.Dart:1171:20) I/flutter ( 4615): #1 Clippath.updateRenderObject (package:flutter/src/widgets/basic.Dart:797:18) I/flutter ( 4615): #2 Renderobjectelement.update (package:flutter/src/widgets/framework.Dart:4510:12) I/flutter ( 4615): #3 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4881:11) I/flutter ( 4615): #4 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #5 Renderobjectelement.updateChildren (package:flutter/src/widgets/framework.Dart:4601:32) I/flutter ( 4615): #6 Multichildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4992:17) I/flutter ( 4615): #7 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #8 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #9 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #10 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #11 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #12 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #13 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #14 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #15 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #16 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #17 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #18 Componentelement.performRebuild (package:flutter/src/widgets/framework.Dart:3750:16) I/flutter ( 4615): #19 Element.rebuild (package:flutter/src/widgets/framework.Dart:3565:5) I/flutter ( 4615): #20 Statefulelement.update (package:flutter/src/widgets/framework.Dart:3894:5) I/flutter ( 4615): #21 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #22 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #23 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #24 Componentelement.performRebuild (package:flutter/src/widgets/framework.Dart:3750:16) I/flutter ( 4615): #25 Element.rebuild (package:flutter/src/widgets/framework.Dart:3565:5) I/flutter ( 4615): #26 Proxyelement.update (package:flutter/src/widgets/framework.Dart:4006:5) I/flutter ( 4615): #27 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #28 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #29 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #30 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #31 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #32 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #33 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #34 Singlechildrenderobjectelement.update (package:flutter/src/widgets/framework.Dart:4883:14) I/flutter ( 4615): #35 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #36 Componentelement.performRebuild (package:flutter/src/widgets/framework.Dart:3750:16) I/flutter ( 4615): #37 Element.rebuild (package:flutter/src/widgets/framework.Dart:3565:5) I/flutter ( 4615): #38 Statelesselement.update (package:flutter/src/widgets/framework.Dart:3796:5) I/flutter ( 4615): #39 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #40 Componentelement.performRebuild (package:flutter/src/widgets/framework.Dart:3750:16) I/flutter ( 4615): #41 Element.rebuild (package:flutter/src/widgets/framework.Dart:3565:5) I/flutter ( 4615): #42 Statefulelement.update (package:flutter/src/widgets/framework.Dart:3894:5) I/flutter ( 4615): #43 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #44 Componentelement.performRebuild (package:flutter/src/widgets/framework.Dart:3750:16) I/flutter ( 4615): #45 Element.rebuild (package:flutter/src/widgets/framework.Dart:3565:5) I/flutter ( 4615): #46 Statefulelement.update (package:flutter/src/widgets/framework.Dart:3894:5) I/flutter ( 4615): #47 Element.updateChild (package:flutter/src/widgets/framework.Dart:2753:15) I/flutter ( 4615): #48 Componentelement.performRebuild (package:flutter/src/widgets/framework.Dart:3750:16) I/flutter ( 4615): #49 Element.rebuild (package:flutter/src/widgets/framework.Dart:3565:5) I/flutter ( 4615): #50 Proxyelement.update.

Does anyone know how I can solve?

  • Which stacktrace is complete? Which line is the error occurring? How about a complete executable code for testing?

  • @Juliohenriquebitencourt entered the full code in the question

1 answer

1


While running your code, the error is in:

@override
bool shouldReclip(CustomClipper<Path> oldClipper) {
  return null;
}

Return false or true instead of null.

That’s why the message

Failed assertion: Boolean Expression must not be null

Note that when updating the date you are updating the whole widget status

if(selecionado != null && selecionado != data) {
  print('Data:  ${selecionado.toString()}');

  setState(() {
    data = selecionado;
  });
}

When updating the status, the CustomClipper executes the method to know if it should redesign the Clipper.

  • 1

    Julio, thank you!!!! I’m starting in the world of Dart now, so I’ll have some "silly" problems like this, solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.