Home > Mobile >  Flutter: Unhandled Exception: type 'Null' is not a subtype of type 'String' when
Flutter: Unhandled Exception: type 'Null' is not a subtype of type 'String' when

Time:08-10

Trying to bulild app but it keeps crashing! When I click on the error it is leading me to two files video.dart and video_controller.dart.

It is pointing me on specific lines but when I click on them there is no specific error explanation of error I get. I tried to look for solution online but I can't understand what is this error telling me! If u know the solution please let me know!

ERROR:

/flutter ( 5006): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'Null' is not a subtype of type 'String'
E/flutter ( 5006): #0      Video.fromSnap
package:tiktok_project/models/video.dart:48
E/flutter ( 5006): #1      VideoController.onInit.<anonymous closure>
package:tiktok_project/controllers/video_controller.dart:19

video.dart :

import 'package:cloud_firestore/cloud_firestore.dart';

class Video {
  String username;
  String uid;
  String id;
  List likes;
  int commentCount;
  int shareCount;
  String songName;
  String caption;
  String videoUrl;
  String thumbnail;
  String profilePhoto;

  Video({
    required this.username,
    required this.uid,
    required this.id,
    required this.likes,
    required this.commentCount,
    required this.shareCount,
    required this.songName,
    required this.caption,
    required this.videoUrl,
    required this.profilePhoto,
    required this.thumbnail,
  });

  Map<String, dynamic> toJson() => {
        "username": username,
        "uid": uid,
        "profilePhoto": profilePhoto,
        "id": id,
        "likes": likes,
        "commentCount": commentCount,
        "shareCount": shareCount,
        "songName": songName,
        "caption": caption,
        "videoUrl": videoUrl,
        "thumbnail": thumbnail,
      };

  static Video fromSnap(DocumentSnapshot snap) {
    var snapshot = snap.data() as Map<String, dynamic>;

    return Video(
      username: snapshot['username'],
      uid: snapshot['uid'],
      id: snapshot['id'],
      likes: snapshot['likes'],
      commentCount: snapshot['commentCount'],
      shareCount: snapshot['shareCount'],
      songName: snapshot['songName'],
      caption: snapshot['caption'],
      videoUrl: snapshot['videoUrl'],
      profilePhoto: snapshot['profilePhoto'],
      thumbnail: snapshot['thumbnail'],
    );
  }
}

video_controller.dart:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:get/get.dart';
import 'package:tiktok_project/const.dart';
import 'package:tiktok_project/models/video.dart';

class VideoController extends GetxController {
  final Rx<List<Video>> _videoList = Rx<List<Video>>([]);

  List<Video> get videoList => _videoList.value;

  @override
  void onInit() {
    super.onInit();
    _videoList.bindStream(
        firestore.collection('videos').snapshots().map((QuerySnapshot query) {
      List<Video> retVal = [];
      for (var element in query.docs) {
        retVal.add(
          Video.fromSnap(element),
        );
      }
      return retVal;
    }));
  }

  likeVideo(String id) async {
    DocumentSnapshot doc = await firestore.collection('videos').doc(id).get();
    var uid = authController.user.uid;
    if ((doc.data() as dynamic)['likes'].contains(uid)) {
      await firestore.collection('videos').doc(id).update({
        'likes': FieldValue.arrayRemove([uid])
      });
    } else {}
    await firestore.collection('videos').doc(id).update({
      'likes': FieldValue.arrayUnion([uid])
    });
  }
}

CodePudding user response:

That's a null safety error.

You need to do something like this

username: snapshot['username'] ?? '',

For each element that is String, or use the type "String?" with the interrogation point telling the compiler that this field may be null

CodePudding user response:

Apparently I can't reproduce the error since I don't have access to your firestore documents, but I am pretty certain that the problem occurs in

...
for (var element in query.docs) {
        retVal.add(
          Video.fromSnap(element),
        );
      }
...

where you attempt to translate element JSON into a Video. In fromSnap, before returning, try printing out snapshot. You should see that it is missing one or more attributes (which is why it was complaining about having a Null when it should be a String), or have some incorrect attributes.

  • Related