1

Estou com um problema na hora de setar o state do DatePicker no Flutter.

O mesmo me retorna o erro:

Another exception was thrown: Failed assertion: boolean expression must not be null

Código:

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

Após alterar a data recebo o seguinte erro:

I/flutter ( 4615): Data: 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: opaque)): 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 (package:flutter/src/widgets/framework.dart:4006:5) I/flutter ( 4615): #51 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #52 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #53 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #54 StatelessElement.update (package:flutter/src/widgets/framework.dart:3796:5) I/flutter ( 4615): #55 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #56 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #57 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #58 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5) I/flutter ( 4615): #59 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #60 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #61 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #62 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5) I/flutter ( 4615): #63 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #64 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4601:32) I/flutter ( 4615): #65 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4992:17) I/flutter ( 4615): #66 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #67 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #68 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #69 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5) I/flutter ( 4615): #70 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #71 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #72 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #73 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5) I/flutter ( 4615): #74 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #75 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #76 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #77 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5) I/flutter ( 4615): #78 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #79 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4883:14) I/flutter ( 4615): #80 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #81 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #82 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #83 StatelessElement.update (package:flutter/src/widgets/framework.dart:3796:5) I/flutter ( 4615): #84 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #85 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4883:14) I/flutter ( 4615): #86 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #87 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #88 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #89 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5) I/flutter ( 4615): #90 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #91 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #92 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #93 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5) I/flutter ( 4615): #94 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #95 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #96 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #97 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5) I/flutter ( 4615): #98 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #99 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #100 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #101 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5) I/flutter ( 4615): #102 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #103 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #104 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #105 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5) I/flutter ( 4615): #106 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:15) I/flutter ( 4615): #107 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16) I/flutter ( 4615): #108 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5) I/flutter ( 4615): #109 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2278:33) I/flutter ( 4615): #110 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:700:20) I/flutter ( 4615): #111 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:286:5) I/flutter ( 4615): #112 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1012:15) I/flutter ( 4615): #113 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:952:9) I/flutter ( 4615): #114 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:864:5) I/flutter ( 4615): #118 _invoke (dart:ui/hooks.dart:219:10) I/flutter ( 4615): #119 _drawFrame (dart:ui/hooks.dart:178:3) I/flutter ( 4615): (elided 3 frames from package dart:async) I/flutter ( 4615): ════════════════════════════════════════════════════════════════════════════════════════════════════ I/flutter ( 4615): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 2169 pos 12: 'attached': is not true.

Alguém sabe como posso resolver?

2
  • Qual o stacktrace completo? Em que linha o erro está ocorrendo? Que tal um código completo executável para testarmos? Commented 19/07/2019 às 19:57
  • @JulioHenriqueBitencourt inseri o código completo na pergunta Commented 19/07/2019 às 20:03

1 Resposta 1

1

Ao rodar seu código, o erro está em:

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

Retorne false ou true ao invés de null.

Por isso a mensagem

Failed assertion: boolean expression must not be null

Note que ao atualizar a data você está atualizando todo o estado do widget

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

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

Ao atualizar o estado, o CustomClipper executa o método para saber se ele deve redesenhar o Clipper.

1
  • 1
    Julio, obrigado!!!! Estou iniciando no mundo do dart agora, então terei alguns problemas "bobos" como esse, resolvido! Commented 22/07/2019 às 13:58

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .