MNBLabs
MNBLabs

Reputation: 95

How to sustain the data in my textfield in flutter?

So I have two textfields in my app and 3 pages. But whenever I navigate to another page the data typed in those textfields are deleted / vanished.

So can you tell me a ways to sustain the typed data. It is like keeping a draft in type box of an email or whatsapp.

main.dart file:

void main() {
      LicenseRegistry.addLicense(() async* {
        final license = await rootBundle.loadString('google_fonts/OFL.txt');
        yield LicenseEntryWithLineBreaks(['google_fonts'], license);
      });
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Colors.teal.shade700,
        statusBarIconBrightness: Brightness.light,
        systemNavigationBarColor: Colors.teal.shade700,
        systemNavigationBarIconBrightness: Brightness.light,
      ));
    
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: AppTheme.appTheme,
          home: OnboardingPage(),
        );
      }
    }

home.dart file:

class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int selectedIndex = 0;
  final screen = [
    ReminderPage(),
    DonePage(),
    HistoryPage(),
  ];

  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;
    return SafeArea(
      child: Scaffold(
        // resizeToAvoidBottomInset: false,
        appBar: AppBar(
          title: const Text('Notiminder'),
          centerTitle: true,
        ),
        body: screen[selectedIndex],
        bottomNavigationBar: Row(// Row...
      ),
    );
  }

  Widget buttomBarItems(IconData icon, int index) {...
}

class BNBCustomPainter extends CustomPainter {...
}

Upvotes: 1

Views: 90

Answers (3)

MNBLabs
MNBLabs

Reputation: 95

We need to create TextEditingController() for the purpose. Simply:

final title = TextEditingController();
final description = TextEditingController();

[You can create it in separate dart file also]

and then Add these controllers to the TextFormFields:

TextFormField(
              controller: title,
),
TextFormField(
              controller: description,
),

This should hopefully do the work...

Upvotes: 0

Husen
Husen

Reputation: 315

you need to save typed text, there are many ways to do this in flutter.

If you have a different version of the application on different platforms or you want to save the last user's typed text, forever, the best way is to use firebase or its alternatives.

if this is not important to you or you do not need it: just use a local database to save the last user's typed texts.

Upvotes: 1

MORE AKASH
MORE AKASH

Reputation: 333

There is only one way to solve is that use state management like getx, provide, or bloc. I prefer you to use getx because it's simpler compared to provider and bloc and also more effective. You can find a bunch of tutorials on getx on Youtube.

Upvotes: 2

Related Questions