Home > OS >  How to sustain the data in my textfield in flutter?
How to sustain the data in my textfield in flutter?

Time:02-25

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 {...
}

CodePudding user response:

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.

CodePudding user response:

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.

  • Related