skip to Main Content

I’m new to flutter and I have this error after running someone else project that I clone. The error is this LateInitializationError : Field 'titleControl' has not been initialized.

I don’t know which code caused the Error. This is the code that I think have something to do with it
upload_widget.dart

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';

import '../services/auth_service.dart';
import '../services/storage_service.dart';
import '../theme/theme.dart';

class UploadPage extends StatefulWidget {
  const UploadPage({Key? key}) : super(key: key);

  @override
  State<UploadPage> createState() => _UploadPageState();
}

class _UploadPageState extends State<UploadPage> {
  late final TextEditingController? titleControl,
      descriptionControl,
      amount,
      dateControl;
  final ImagePicker _picker = ImagePicker();
  final FocusNode title = FocusNode();
  final FocusNode description = FocusNode();
  final FocusNode date = FocusNode();
  final formKey = GlobalKey<FormState>();
  File? _image;
  late String url;
  var uuid = 'Uuid';

  @override
  void initState() {
    super.initState();
  }

This is the code that I think related to titleControl.
This is the repository that I copy the code from : https://github.com/gdsc-tknp/thriftify/blob/main/lib/widgets/upload_widget.dart
The file of the error that I clone.

2

Answers


  1. You must initialize all the TextEditingControllers before you try to use them. This can be done in the init block :

     @override
      void initState() {
        super.initState();
        titleControl = TextEditingController();
    ...
      }
    
    

    and don’t forget to dispose them :

    @override
      void dispose() {
        titleControl.dispose();
    ...
        super.dispose();
      }
    
    
    Login or Signup to reply.
  2. The late modifier is the culprit.
    TextEditingControllers (titleControl,descriptionControl, amount, dateControl) should be initialized in the initState method.

    Eg.

    @override
    void initState() {
    titleControl = TextEditingController(); // descriptionControl, amount, dateControl as well
    super.initState();
    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search