Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

flutter - Why am I getting an error in int map operation during firebase connection? as String' is not a subtype of type 'int'

during firebase connection, I get an error type 'String' is not a subtype of type 'int'


class _VeriTabaniState extends State<VeriTabani> {
  @override
  Widget build(BuildContext context) {
    return _buildBody(context, sahteSnapshot);
  }

  Widget _buildBody(BuildContext context, List<Map> snapshot) {
    return ListView(
      padding: EdgeInsets.only(top: 25.0),
      //buildListItem ztn bir liste, children i?ine al?r direk
      children:
          //sahte datalardan gelen her?ey i?in d?ndürece?im
          //buildListItem'? creat method ile olu?turdum
          snapshot
              .map<Widget>((data) => _buildListItem(context, data))
              .toList(),
    );
  }

  _buildListItem(BuildContext context, Map data) {
    //sahtelerden her gelen i?in yeni bir kay?t üretecek
    final kayitWidget = Kayit.fromMap(data);
    return Padding(
      //biz bunlara t?klar?z m?klar?z, hangisinin oldu?unu anlamak i?in key
      key: ValueKey(kayitWidget.okulisim),
      padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.2),
      child: Container(
        decoration: BoxDecoration(
            border: Border.all(color: Colors.blueAccent),
            borderRadius: BorderRadius.circular(5.0)),
        child: ListTile(
          title: Text(kayitWidget.okulisim),
          trailing: Text(kayitWidget.sayi.toString()),
          //firebase'e yollayaca??z
          onTap: () => {},
        ),
      ),
    );
  }
}

//sahte veriler olu?tural?m
final sahteSnapshot = [
  {"okulisim": "Sau", "sayi": "1"},
  {"okulisim": "Metu", "sayi": "2"},
  {"okulisim": "Kou", "sayi": "3"},
  {"okulisim": "Boun", "sayi": "4"},
];

class Kayit {
  String okulisim;
  String sayi;
  DocumentReference reference;

  //kayit icin bir constructer olu?tural?m
  //fb icin ?ok kullan?lan bir yap?s? var bunlar?n
  Kayit.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map["okulisim"] != null),
        assert(map["sayi"] != null),
        okulisim = map["okulisim"],
        sayi = map["sayi"];

  //dbdeki anl?k de?isimler
  Kayit.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data(), reference: snapshot.reference);

  @override
  String toString() => "Kay?t<$okulisim : $sayi>";
}

is actually where I get the int String error:

Kayit.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map["okulisim"] != null),
        assert(map["sayi"] != null),
        okulisim = map["okulisim"],
        sayi = map["sayi"];

and it fixes when I make the variable int to String

class Kayit {
  String okulisim;
  String sayi;
  DocumentReference reference;

  //kayit icin bir constructer olu?tural?m
  //fb icin ?ok kullan?lan bir yap?s? var bunlar?n
  Kayit.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map["okulisim"] != null),
        assert(map["sayi"] != null),
        okulisim = map["okulisim"],
        sayi = map["sayi"];

How?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

String' is not a subtype of type 'int'

You are retrieving a String from the database and assigning it to an int, thus you get the above error. To solve this either change your variable to type String or save an int in the database:

final sahteSnapshot = [
  {"okulisim": "Sau", "sayi": 1},
  {"okulisim": "Metu", "sayi": 2},
  {"okulisim": "Kou", "sayi": 3},
  {"okulisim": "Boun", "sayi": 4},
];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...