簡體   English   中英

'package:flutter/src/paintings/_network_image_io.dart':斷言失敗:第 25 行 pro 14:'url:= null'。 不是真的。StreamBuilder

[英]'package:flutter/src/paintings/_network_image_io.dart':Failed assertion: line 25 pros 14: 'url != null': is not true.StreamBuilder

我正在嘗試使用 flutter 構建一個社交媒體應用程序,我已經創建了一個登錄頁面和登錄頁面,它也需要用戶頭像、用戶名、用戶電子郵件、用戶密碼。 當單擊浮動操作按鈕作為提交按鈕時,它將注冊帳戶,但在我的情況下,它只注冊 email、useruid、username 和 userpassord 而不是 userimage 它顯示這個'package:flutter/src/paintings/_network_image_io.dart':斷言失敗:第 25 行優點 14:'url:= null'。 不是真的。導致小部件的相關錯誤是 stream 構建器...這是我的錯誤..任何人都可以幫助我將我的代碼留在下面。

LandingServices 作為登錄並登錄

class LandingService with ChangeNotifier {
TextEditingController userEmailController = TextEditingController();
TextEditingController userNameController = TextEditingController();
 TextEditingController userPasswordController = TextEditingController();
 ConstantColors constantColors = ConstantColors();

顯示用戶頭像

  showUserAvatar(BuildContext context) {
return showModalBottomSheet(
    context: context,
    builder: (context) {
      return Container(
        height: MediaQuery.of(context).size.height * 0.36,
        width: MediaQuery.of(context).size.width,
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 150.0),
              child: Divider(
                thickness: 4.0,
                color: constantColors.whiteColor,
              ),
            ),
            CircleAvatar(
              radius: 69.0,
              backgroundColor: constantColors.transperant,
              backgroundImage: FileImage(
                Provider.of<LandingUtils>(context, listen: false).userAvatar
              )),
            Container(
              child: 
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  MaterialButton(
                      child: Text(
                        'Reselect',
                        style: TextStyle(
                            color: constantColors.whiteColor,
                            fontWeight: FontWeight.bold,
                            decoration: TextDecoration.underline,
                            decorationColor: constantColors.whiteColor),
                      ),
                      onPressed: () {
                        Provider.of<LandingUtils>(context, listen: false)
                            .pickUserAvatar(context, ImageSource.gallery);
                      }),
                  MaterialButton(
                      color: constantColors.blueColor,
                      child: Text(
                        'Confirm Image',
                        style: TextStyle(
                          color: constantColors.whiteColor,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      onPressed: () {
                        Provider.of<FirebaseOperations>(context,
                                listen: false)
                            .uploadUserAvatar(context)
                            .whenComplete(() {
                          signInSheet(context);
                        });
                      }),
                ],
              ),
            )
          ],
        ),
        decoration: BoxDecoration(
          color: constantColors.blueGreyColor,
          borderRadius: BorderRadius.circular(15.0),
        ),
      );
    });
   }

無密碼登錄

  Widget passswordLessSignIn(BuildContext context) {
return SizedBox(
  height: MediaQuery.of(context).size.height * 0.40,
  width: MediaQuery.of(context).size.width,
  child: StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance.collection('users').snapshots(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting) {
        return Center(
          child: CircularProgressIndicator(),
        );
      } else {
        return new ListView(
            children:
                snapshot.data.docs.map((DocumentSnapshot documentSnapshot) {
          return ListTile(
            trailing: IconButton(
              icon: Icon(FontAwesomeIcons.trashAlt,
                  color: constantColors.redColor),
              onPressed: () {},
            ),
            leading: CircleAvatar(
              backgroundColor: constantColors.transperant,
              backgroundImage:
                  NetworkImage(documentSnapshot.data()['userimage']),
            ),
            subtitle: Text(documentSnapshot.data()['useremail'],
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: constantColors.greenColor,
                    fontSize: 12.0)),
            title: Text(documentSnapshot.data()['username'],
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: constantColors.greenColor)),
          );
        }).toList());
      }
    },
  ),
);
  }

簽到表

 signInSheet(BuildContext context) {
return showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    builder: (context) {
      return Padding(
        padding: EdgeInsets.only(
            bottom: MediaQuery.of(context).viewInsets.bottom),
        child: Container(
          height: MediaQuery.of(context).size.height * 0.5,
          width: MediaQuery.of(context).size.width,
          decoration: BoxDecoration(
              color: constantColors.blueGreyColor,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(12.0),
                  topRight: Radius.circular(12.0))),
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 150.0),
                child: Divider(
                  thickness: 4.0,
                  color: constantColors.whiteColor,
                ),
              ),
              CircleAvatar(
                backgroundImage: FileImage(
                    Provider.of<LandingUtils>(context, listen: false)
                        .getUserAvatar),
                backgroundColor: constantColors.redColor,
                radius: 60.0,
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 10.0),
                child: TextField(
                  controller: userNameController,
                  decoration: InputDecoration(
                    hintText: 'Enter name...',
                    hintStyle: TextStyle(
                      color: constantColors.whiteColor,
                      fontWeight: FontWeight.bold,
                      fontSize: 16.0,
                    ),
                  ),
                  style: TextStyle(
                    color: constantColors.whiteColor,
                    fontWeight: FontWeight.bold,
                    fontSize: 18.0,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 10.0),
                child: TextField(
                  controller: userEmailController,
                  decoration: InputDecoration(
                    hintText: 'Enter Email...',
                    hintStyle: TextStyle(
                      color: constantColors.whiteColor,
                      fontWeight: FontWeight.bold,
                      fontSize: 16.0,
                    ),
                  ),
                  style: TextStyle(
                    color: constantColors.whiteColor,
                    fontWeight: FontWeight.bold,
                    fontSize: 18.0,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 10.0),
                child: TextField(
                  controller: userPasswordController,
                  decoration: InputDecoration(
                    hintText: 'Enter Password...',
                    hintStyle: TextStyle(
                      color: constantColors.whiteColor,
                      fontWeight: FontWeight.bold,
                      fontSize: 16.0,
                    ),
                  ),
                  style: TextStyle(
                    color: constantColors.whiteColor,
                    fontWeight: FontWeight.bold,
                    fontSize: 18.0,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 0.32),
                child: FloatingActionButton(
                    backgroundColor: constantColors.redColor,
                    child: Icon(
                      FontAwesomeIcons.check,
                      color: constantColors.whiteColor,
                    ),
                    onPressed: () {
                      if (userEmailController.text.isNotEmpty) {
                        Provider.of<Authentication>(context, listen: false)
                            .createAccount(userEmailController.text,
                                userPasswordController.text)
                            .whenComplete(() {
                          print('Creating collections...');
                          Provider.of<FirebaseOperations>(context,
                                  listen: false)
                              .createUserCollection(context, {
                            'useruid': Provider.of<Authentication>(context,
                                    listen: false)
                                .getUserUid,
                            'useremail': userEmailController.text,
                            'username': userNameController.text,
                            'userimage': Provider.of<LandingUtils>(context,
                                    listen: false)
                                .getUserAvatarUrl,
                          });
                        }).whenComplete(() {
                          Navigator.pushReplacement(
                              context,
                              PageTransition(
                                  child: Homepage(),
                                  type: PageTransitionType.bottomToTop));
                        });
                      } else {
                        warningText(context, 'Fill all the data!');
                      }
                    }),
              ),
            ],
          ),
        ),
      );
    });
    }

firebaseOperation.dart

import 'package:cloud_firestore/cloud_firestore.dart';
   import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
 import 'package:theGupshup/screens/LandingPage/LandingUtils.dart';
  import 'package:theGupshup/services/Authentication.dart';

   class FirebaseOperations with ChangeNotifier {
      UploadTask imageUploadTask;
      String initUserName, initUserEmail, initUserImage;
      String get getInitUserName => initUserName;
      String get getInitUserEmail => initUserEmail;
      String get getInitUserImage => initUserImage;
      Future uploadUserAvatar(BuildContext context) async {
      Reference imageReference = FirebaseStorage.instance.ref().child(
         'userProfileAvatar/${Provider.of<LandingUtils>(context, listen: 
          false).getUserAvatar.path}/${TimeOfDay.now()}');
      imageUploadTask = imageReference.putFile(
         Provider.of<LandingUtils>(context, listen: false).getUserAvatar);

        await imageUploadTask.whenComplete(() {
        print('Image uploaded!');
         });
        imageReference.getDownloadURL().then((url) {
         Provider.of<LandingUtils>(context, listen: false).userAvatarUrl =
            url.toString();
          print(
             'the user profile avatar url => ${Provider.of<LandingUtils>(context, listen: 
                 false).userAvatarUrl}');
                  notifyListeners();
                          });
                           }

               Future createUserCollection(BuildContext context, dynamic data) async {
                  return FirebaseFirestore.instance
                      .collection('users')
                 .doc(Provider.of<Authentication>(context, listen: false).getUserUid)
                  .set(data);
                       }

                  Future initUserData(BuildContext context) async {
                     return FirebaseFirestore.instance
                      .collection('users')
                       .doc(Provider.of<Authentication>(context, listen: false).getUserUid)
                          .get()
                            .then((doc) {
                                  print('Fetching user data');
                       initUserName = doc.data()['username'];
                       initUserEmail = doc.data()['useremail'];
                        initUserImage = doc.data()['userimage'];
                           print(initUserName);
                            print(initUserEmail);
                               print(initUserImage);
                                    notifyListeners();
                                         });
                                        }
                                   } 

登陸utils.dart

class LandingUtils with ChangeNotifier {
ConstantColors constantColors = ConstantColors();
 final picker = ImagePicker();
 File userAvatar;
  File get getUserAvatar => userAvatar;
 String userAvatarUrl;
  String get getUserAvatarUrl => userAvatarUrl;

  Future pickUserAvatar(BuildContext context, ImageSource source) async {
   final pickedUserAvatar = await picker.getImage(source: source);
    pickedUserAvatar == null
         ? print('Select image')
       : userAvatar = File(pickedUserAvatar.path);
         print(userAvatar.path);

            userAvatar != null
            ? Provider.of<LandingService>(context, listen: false)
                 .showUserAvatar(context)
             : print('Image Upload error');
                notifyListeners();
                       }
       
               Future selectAvatarOptionSheet(BuildContext context) async {
return showModalBottomSheet(
    context: context,
    builder: (context) {
      return Container(
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 150.0),
              child: Divider(
                thickness: 4.0,
                color: constantColors.whiteColor,
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                MaterialButton(
                    color: constantColors.blueColor,
                    child: Text('Gallery',
                        style: TextStyle(
                            color: constantColors.whiteColor,
                            fontWeight: FontWeight.bold,
                            fontSize: 18.0)),
                    onPressed: () {
                      pickUserAvatar(context, ImageSource.gallery)
                          .whenComplete(() {
                        Navigator.pop(context);
                        Provider.of<LandingService>(context, listen: false)
                            .showUserAvatar(context);
                      });
                    }),
                MaterialButton(
                    color: constantColors.blueColor,
                    child: Text('Camera',
                        style: TextStyle(
                            color: constantColors.whiteColor,
                            fontWeight: FontWeight.bold,
                            fontSize: 18.0)),
                    onPressed: () {
                      pickUserAvatar(context, ImageSource.camera)
                          .whenComplete(() {
                        Navigator.pop(context);
                        Provider.of<LandingService>(context, listen: false)
                            .showUserAvatar(context);
                      });
                    }),
              ],
            ),
          ],
        ),
        height: MediaQuery.of(context).size.height * 0.1,
        width: MediaQuery.of(context).size.width,
        decoration: BoxDecoration(
            color: constantColors.blueGreyColor,
            borderRadius: BorderRadius.circular(12.0)),
      );
    });
           }
           }

任何人都可以幫助錯誤圖片如下

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

任何人都可以幫助

在您的密碼LessSignIn 頁面中,當用戶圖像為 null 時,嘗試為圓形頭像提供默認圖像 -

例子 -

 leading: CircleAvatar(
              backgroundColor: constantColors.transperant,
              backgroundImage:
                  NetworkImage(documentSnapshot.data()['userimage'] ?? "https://i.guim.co.uk/img/media/7a633730f5f90db3c12f6efc954a2d5b475c3d4a/0_138_5544_3327/master/5544.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=27c09d27ccbd139fd0f7d1cef8f7d41d"),
            ),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM