Home > Enterprise >  Dart Constructor gives me null values
Dart Constructor gives me null values

Time:03-11

I want to pass data from child to parent widget but I can't use the provider so I tried passing the values to a new class constructor and then using it where ever I want but that didn't go very well for methis is the screen I want to pass values to the text field then update it in the firebase but it returns null

// this is the code that run when pressing the button    
updatefn: (){
                        Data(
                          textFieldName: controllerName,
                          textFieldImage: controllerImage,
                          textFieldDetails: controllerDetails,
                          textFieldLongitude: controllerLon,
                          textFieldLatitude: controllerLat
                        );
                        Data().printText();
                        collect.reference.update({
                          'Name': Data().textFieldName.toString(),
                          'Image': Data().textFieldImage.toString(),
                          'details':Data().textFieldDetails.toString(),
                        }).whenComplete(() => Navigator.pop(context));
                        print("updated");
                      },
//and this is the class
class Data{
  Data({this.textFieldDetails,this.textFieldImage,
      this.textFieldLatitude,this.textFieldLongitude,this.textFieldName,
      this.currentLat,this.currentLon});

  final textFieldLatitude;
  final textFieldLongitude;
  final textFieldName;
  final textFieldImage;
  final textFieldDetails;
}

CodePudding user response:

You're not assigning the Data you constructed to anything, you're creating a new variable each time you call Data(), so all values are null. It should be something like this:

var data = Data(
   textFieldName: controllerName,
   textFieldImage: controllerImage,
   textFieldDetails: controllerDetails,
   textFieldLongitude: controllerLon,
   textFieldLatitude: controllerLat
);

data.printText();
collect.reference.update({
   'Name': data.textFieldName.toString(),
   'Image': data.textFieldImage.toString(),
   'details':data.textFieldDetails.toString(),
   }).whenComplete(() => Navigator.pop(context));
   print("updated");
   },
  • Related