Search code examples
fluttertexteditingcontroller

Flutter TextEditingController - Disposal & Initiation Requirements?


I am creating 100's of TextEditing Controllers in my app. Here's what im doing:

TextEditingController _customerName = TextEditingController();

I'm just curious, but if I have 100s of these elements, am I meant to do anything in

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

or

@override
void dispose()
{... } 

???

Im just asking as my app is sluggish and I have a feeling it maybe because I'm using 1000s of these controllers.

Thanks


Solution

  • You should dispose every TextEditingController before disposing its screen. For example:

    @override
    void dispose() {
        _customerName.dispose();
        super.dispose();
    }
    

    Docs: https://api.flutter.dev/flutter/widgets/TextEditingController-class.html