dart 必须初始化不可为null的变量“size”,请尝试添加初始值设定项表达式

e0bqpujr  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(182)

我尝试开发一个菜谱收集应用,这是菜谱添加界面,但是出现错误:“必须初始化不可为null的变量'size'。请尝试添加初始值设定项表达式。”和“必须初始化不可为null的变量'image'。请尝试添加初始值设定项表达式。”。
如何初始化尺寸和图像?
addpage.dart

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:intl/intl.dart';
import 'package:image_picker/image_picker.dart';

File image; //https://stackoverflow.com/questions/67610330/the-name-file-is-defined-in-the-libraries-darthtml-and-dartio
// String filename;

class CommonThings {
  static Size size;
}

class RecipeAddPage extends StatefulWidget {
  RecipeAddPage({super.key});

  @override
  State<RecipeAddPage> createState() => _RecipeAddPageState();
}

class _RecipeAddPageState extends State<RecipeAddPage> {
  late TextEditingController nameInputController;
  late TextEditingController recipeDescriptionController;
  late TextEditingController imageInputController;
  late TextEditingController recipeTimePreparedController;
  late TextEditingController recipeNumberServeController;
  late TextEditingController recipeIngredientController;
  late TextEditingController recipeStepController;
  late TextEditingController recipeNoteController;
  late TextEditingController recipeCategoriesController;

  late String recipe_id;
  //-------------------------------------------------/
  //Firestore implement later On                     /
  //final db = Firestore.instance;
  //-------------------------------------------------/
  final _formKey = GlobalKey<FormState>();
  late String recipeName;
  late String recipeDesc;
  late String recipeCatgories;
  late String recipeTimePrepared;
  late String recipeServe;
  late String recipeIngredient;
  late String recipeStep;
  late String recipeNote;

  pickerCam() async {
    File img = await ImagePicker.pickImage(source: ImageSource.camera);
    if (img != null) {
      image = img;
      setState(() {});
    }
  }

  pickerGallery() async {
    File img = await ImagePicker.pickImage(source: ImageSource.gallery);
    if (img != null) {
      image = img;
      setState(() {});
    }
  }

  Widget divider() {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 4.0),
      child: Container(
        width: 0.8,
        color: Colors.black,
      ),
    );
  }

  //To store the information in the firebase - need time
  // Integrate with the firebase
  //implement later
  // void createData() async {
  //   DateTime now = DateTime.now();
  //   String nuevoformato = DateFormat('kk:mm:ss:MMMMd').format(now);
  //   var fullImageName = 'nomfoto-$nuevoformato' + '.jpg';
  //   var fullImageName2 = 'nomfoto-$nuevoformato' + '.jpg';

  //   final StorageReference ref =
  //       FirebaseStorage.instance.ref().child(fullImageName);
  //   final StorageUploadTask task = ref.putFile(image);

  //   var part1 =
  //       'https://firebasestorage.googleapis.com/v0/b/apprecetas-cfd25.appspot.com/o/';

  //   var fullPathImage = part1 + fullImageName2;
  //   print(fullPathImage);

  //   if (_formKey.currentState!.validate()) {
  //     _formKey.currentState!.save();
  //     DocumentReference ref = await db.collection('colrecipes').add({
  //       'recipeName': '$recipeName',
  //       'recipeDesc': '$recipeDesc',
  //       'recipeCatgories': '$recipeCatgories',
  //       'recipeTimePrepared': '$recipeTimePrepared',
  //       'recipeServe': '$recipeServe',
  //       'recipeIngredient': '$recipeIngredient',
  //       'recipeIngredient': '$recipeStep',
  //       'recipeNote': '$recipeNote',
  //       'image': '$fullPathImage'
  //     });

  //     setState(() => recipe_id = ref.documentID);
  //     Navigator.of(context).pop();
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    CommonThings.size = MediaQuery.of(context).size;

    return Scaffold(
        appBar: AppBar(
          title: Text('Add Recipe'),
        ),
        body: ListView(
          padding: EdgeInsets.all(8),
          children: <Widget>[
            Form(
                key: _formKey,
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        new Container(
                          height: 200.0,
                          width: 200.0,
                          decoration: new BoxDecoration(
                            border: new Border.all(color: Colors.blueAccent),
                          ),
                          padding: new EdgeInsets.all(5.0),
                          child:
                              image == null ? Text('Add') : Image.file(image),
                        ),
                        Divider(),
                        new IconButton(
                            icon: new Icon(Icons.camera_alt),
                            onPressed: pickerCam()),
                        Divider(),
                        new IconButton(
                            icon: new Icon(Icons.image),
                            onPressed: pickerGallery()),
                      ],
                    ),
                    Container(
                      child: TextFormField(
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: 'Recipe Name...',
                          fillColor: Colors.grey[300],
                          filled: true,
                        ),
                        validator: (value) {
                          if (value!.isEmpty) {
                            return 'Recipe Name...';
                          }
                        },
                        onSaved: (value) => recipeName = value!,
                      ),
                    ),
                    Container(
                      child: TextFormField(
                        maxLines: 10,
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: 'Recipes Description...',
                          fillColor: Colors.grey[300],
                          filled: true,
                        ),
                        validator: (value) {
                          if (value!.isEmpty) {
                            return 'Recipes Description...';
                          }
                        },
                        onSaved: (value) => recipeDesc = value!,
                      ),
                    ),
                    //                              //
                    // Insert the recipe categories //
                    //                              //
                    Container(
                      child: TextFormField(
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: 'Eg: 60m/120m/180m/240m',
                          fillColor: Colors.grey[300],
                          filled: true,
                        ),
                        validator: (value) {
                          if (value!.isEmpty) {
                            return 'Eg: 60m/120m/180m/240m';
                          }
                        },
                        onSaved: (value) => recipeTimePrepared = value!,
                      ),
                    ),
                    Container(
                      child: TextFormField(
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: 'Eg: 2 peoples ',
                          fillColor: Colors.grey[300],
                          filled: true,
                        ),
                        validator: (value) {
                          if (value!.isEmpty) {
                            return 'Eg: 2 peoples';
                          }
                        },
                        onSaved: (value) => recipeServe = value!,
                      ),
                    ),
                    Container(
                      child: TextFormField(
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: 'List of ingredients... ',
                          fillColor: Colors.grey[300],
                          filled: true,
                        ),
                        validator: (value) {
                          if (value!.isEmpty) {
                            return 'List of ingredients... ';
                          }
                        },
                        onSaved: (value) => recipeIngredient = value!,
                      ),
                    ),
                    Container(
                      child: TextFormField(
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: 'Step description...',
                          fillColor: Colors.grey[300],
                          filled: true,
                        ),
                        validator: (value) {
                          if (value!.isEmpty) {
                            return 'Step description...';
                          }
                        },
                        onSaved: (value) => recipeStep = value!,
                      ),
                    ),
                    Container(
                      child: TextFormField(
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: 'Step description...',
                          fillColor: Colors.grey[300],
                          filled: true,
                        ),
                        validator: (value) {
                          if (value!.isEmpty) {
                            return 'Step description...';
                          }
                        },
                        onSaved: (value) => recipeStep = value!,
                      ),
                    ),
                    Container(
                      child: TextFormField(
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: 'Notes description...',
                          fillColor: Colors.grey[300],
                          filled: true,
                        ),
                        validator: (value) {
                          if (value!.isEmpty) {
                            return 'Notes description...';
                          }
                        },
                        onSaved: (value) => recipeNote = value!,
                      ),
                    )
                  ],
                )),
            Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  ElevatedButton(
                    onPressed: () {},
                    // createData,
                    child:
                        Text('Create', style: TextStyle(color: Colors.white)),
                  ),
                ])
          ],
        ));
  }
}
zrfyljdw

zrfyljdw1#

您可以通过向其添加?使其可为空,例如

File? image; //https://stackoverflow.com/questions/67610330/the-name-file-is-defined-in-the-libraries-darthtml-and-dartio
// String filename;

class CommonThings {
  static Size? size;
}

相关问题