Home > Mobile >  Bug Null check operator used on a null value in flutter
Bug Null check operator used on a null value in flutter

Time:08-18

I have a specialDialog in dart that when clicking on the FlatButton displays an error message in the emulator "The relevant error-causing widget was ScheduleReturn"

  specialDialog(BuildContext context, String? agent_id) {
// set up the buttons
Widget coverageButton = FlatButton(
  child: Text("Solicitar cobertura"),
  onPressed: () {
    localRequestCoverage(agent_id);
  },
);
Widget returnButton = FlatButton(
  child: Text("Solicitar devolución"),
  onPressed: () {
    Route route = MaterialPageRoute(
        builder: (context) => ScheduleReturn(schedule: widget.schedule));
    Navigator.pushReplacement(context, route);
  },
);

CodePudding user response:

I include ScheduleReturn code:

import 'package:flutter/material.dart';
import 'package:sig/listview/schedule_return_listview.dart';

class ScheduleReturn extends StatefulWidget {

  ScheduleReturn({Key? key, this.schedule, this.day, this.day_name, this.month_name, this.schedule_change}) : super(key: key);
  final String? schedule;
  final String? day;
  final String? day_name;
  final String? month_name;
  final String? schedule_change;

  @override
  _ScheduleReturn createState() => _ScheduleReturn();

}

class _ScheduleReturn extends State<ScheduleReturn> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
          backgroundColor: Colors.black87,
          title: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                'Devolución de horario',
                style: TextStyle(color: Colors.white, fontSize: 16.0),
              ),
              Text(
                widget.day_name!   ' '   widget.day!   ' '   widget.month_name!   ' '   widget.schedule_change!,
                style: TextStyle(fontSize: 10, fontWeight: FontWeight.w500),
              )
            ],
          ),
        ),
        body: Column(
          children: [
            new Expanded(
              child:Container(
                  margin: const EdgeInsets.only(top: 0.0),
                  child: new Center(
                    child: ScheduleReturnListView(schedule: widget.schedule),

CodePudding user response:

change this :

Text(
                widget.day_name!   ' '   widget.day!   ' '   widget.month_name!   ' '   widget.schedule_change!,
                style: TextStyle(fontSize: 10, fontWeight: FontWeight.w500),
              )

to this:

Text(
               '${widget.day_name}'   ' '   '${widget.day}'   ' '   '${widget.month_name}'   ' '   '${widget.schedule_change}',
                style: TextStyle(fontSize: 10, fontWeight: FontWeight.w500),
              )

CodePudding user response:

In code, Wherever you use agent_id with !(null-aware-operator), at that place you got null for agent id. You are getting null value, where you are using !(null-aware-operator) to unwrap the data.

So, You need to check value passing at that line in code.

CodePudding user response:

Instead of using ! directly do a null check before it. also you can provide default value on null case like

 Text( (widget.day_name??"")   ' '   (widget.day?? "")    ' '   (widget.month_name??"")    ' '   ( widget.schedule_change??""),

Or better directly use String format.

Text(
  "${widget.day_name} ${widget.day} ",

Also, you can assign value on null case

  • Related