Emir Bolat
Emir Bolat

Reputation: 1049

The error I get while saving the information entered in the Dart Flutter text input field

When watching the trainer's video, she doesn't get such an error, but while I'm typing.

Instructor:

enter image description here


My Code:

enter image description here

What is the problem?

My Related code:

userAdd.dart:

import 'dart:io';


import 'package:denemeleruygulamasi/personel.dart';
import 'package:flutter/material.dart';

class personelEkle extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return personelEkleState();
  }
}

class personelEkleState extends State{
  var personel = personelBilgileri.bilgileri();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Personel Ekle"),
      ),

      body: Container(
        margin: EdgeInsets.all(15),
        child: Form(
          child: Column(
            children: <Widget>[
              TextFormField(
                decoration: InputDecoration(
                  labelText: "Personel adı",
                  hintText: "Ad - soyad",
                ),
                onSaved: (String? value) {
                  personel.ad = value;
                },
              ),

              TextFormField(
                decoration: InputDecoration(
                  labelText: "Personel soyadı",
                  hintText: "Ad - soyad",
                ),
              ),

              TextFormField(
                decoration: InputDecoration(
                  labelText: "Personel kıdem yılı",
                  hintText: "Ad - soyad",
                ),
              ),

            ],
          ),
        ),
      ),
    );

  }
}

personel.dart:

class personelBilgileri{
  late int? id;
  late String ad;
  late String soyad;
  late int kidem;
  late String unvan;

  personelBilgileri.withId(this.id, this.ad, this.soyad, this.kidem, this.unvan);

  personelBilgileri(this.ad, this.soyad, this.kidem, this.unvan);

  personelBilgileri.bilgileri();
  
String get unvanGet{
  String mesaj = "";
  if (this.kidem <= 3){
      mesaj = "Pro";
  }

  else if (this.kidem <= 5) {
    mesaj = "Expert";
  } 

  else {
    mesaj = "Expert Pro";
  }

  return mesaj;
}
}

personel = staff.

Error:

String? value
A value of type 'String?' can't be assigned to a variable of type 'String'.
Try changing the type of the variable, or casting the right-hand type to 'String'.dartinvalid_assignment

I'm trying to make a staff application. I'm currently having a problem with adding staff. I created a separate dart file to add staff.

Upvotes: 0

Views: 54

Answers (1)

Basel Abuhadrous
Basel Abuhadrous

Reputation: 1660

I guess you are using null safety and the video is old enough that it doesn't use it, please try the following:

onSaved: (String? value) {
  personel.ad = value;
},

Upvotes: 1

Related Questions