flutter 类型“Null”不是“image”抖动问题的“File”类型的子类型

tzxcd3kk  于 2023-04-07  发布在  Flutter
关注(0)|答案(2)|浏览(116)

我上传了我的图片到FireStore。当我创建一个图片时,一切都很好。在注册后,我可以轻松地转到用户配置文件,可以看到用户名和电子邮件。但是当我从用户配置文件和我的LOGIN中注销时,提供我已经创建的现有电子邮件和密码,然后抛出Exception。我没有得到这个问题。发生异常。_TypeError(类型'Null'不是'image'的类型'File'的子类型)

import 'dart:io';

import '../picker/user_image_picker.dart';
import 'package:flutter/material.dart';

class AuthForm extends StatefulWidget {
  AuthForm(this.submitFn, this.isLoading);
  final bool isLoading;
  var submitFn;
  void function(
    String email,
    String password,
    String userName,
    File image,
    bool isLogin,
    BuildContext ctx,
  );

  @override
  _AuthFormState createState() => _AuthFormState();

  @override
  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

class _AuthFormState extends State<AuthForm> {
  final _formKey = GlobalKey<FormState>();
  var _isLogin = true;
  var _userEmail = '';
  var _userName = '';
  var _userPasssword = '';
   File? userImageFile;
  void pickedImage(File image) {
    userImageFile = image;
  }

  void _trySubmit() {
    final isValid = _formKey.currentState!.validate();
    FocusScope.of(context).unfocus();
    if (userImageFile == null && !_isLogin) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('Please Pick an Image.'),
          backgroundColor: Theme.of(context).errorColor,
        ),
      );
      return;
    }

    if (isValid) {
      _formKey.currentState?.save();
      print(_userEmail);
      print(_userName);
      print(_userPasssword);
      print(' Good to go ');
      widget.submitFn(
        _userEmail.trim(),
        _userPasssword.trim(),
        _userName.trim(),
        userImageFile,
        _isLogin,
        context,
      );
    } else {
      print('Form is not valid');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.all(20),
      child: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(16),
          child: Form(
            key: _formKey,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                if (!_isLogin) UserImagePicker(pickedImage),//this is my image file
                TextFormField(
                  key: ValueKey('email'),
                  onSaved: (value) {
                    _userEmail = value!;
                  },
                  validator: (value) {
                    if (value!.isEmpty ||
                        !value.contains(RegExp(
                            '^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+.[a-z]+.[com]'))) {
                      return 'enter valid Email';
                      //print('Email validation is not ok');
                    } else {
                      return null;
                    }
                  },
                  keyboardType: TextInputType.emailAddress,
                  decoration: InputDecoration(
                      labelText: 'Email Address', icon: Icon(Icons.email)),
                ),
                if (!_isLogin)
                  TextFormField(
                    key: ValueKey('userName'),
                    onSaved: (value) {
                      _userName = value!;
                    },
                    validator: (value) {
                      if (value == null || value.length < 4) {
                        return 'Enter At least 4 charaters ';
                      }
                      return null;
                    },
                    decoration: InputDecoration(
                        labelText: 'User Name', icon: Icon(Icons.person)),
                  ),
                TextFormField(
                  key: ValueKey('password'),
                  onSaved: (value) {
                    _userPasssword = value!;
                  },
                  obscureText: true,
                  validator: (value) {
                    if (value == null || value.isEmpty || value.length < 7) {
                      return 'Enter at least 7 Digits';
                    }
                    return null;
                  },
                  decoration: InputDecoration(
                    labelText: 'Password',
                    icon: Icon(
                      Icons.security,
                    ),
                  ),
                ),
                SizedBox(
                  height: 20,
                ),
                if (widget.isLoading) CircularProgressIndicator(),
                if (!widget.isLoading)
                  ElevatedButton(
                    child: Text(_isLogin ? 'Login' : 'Signup'),
                    onPressed: _trySubmit,
                  ),
                if (widget.isLoading) CircularProgressIndicator(),
                if (!widget.isLoading)
                  TextButton(
                    style: TextButton.styleFrom(
                      primary: Theme.of(context).primaryColor,
                    ),
                    child: Text(_isLogin
                        ? 'Create New Account'
                        : 'I already have an account'),
                    onPressed: () {
                      setState(
                        () {
                          _isLogin = !_isLogin;
                        },
                      );
                    },
                  )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

vvppvyoh

vvppvyoh1#

在将userImageFile发送/传递到submitFn函数之前,请确保userImageFile不是emptyNULL

if(userImageFile.isNotEmpty() || userImageFile!=null){
     widget.submitFn(
            _userEmail.trim(),
            _userPasssword.trim(),
            _userName.trim(),
            userImageFile,
            _isLogin,
            context,
          );
}
b09cbbtk

b09cbbtk2#

看起来userImageFilenull。这是因为这个变量是可以为空的,当它传递给submitFn(奇怪的是,它是一个dynamic变量...?)时,它会抛出,因为它需要一个不可为空的image参数。

相关问题