How to retrieve text value from RichText in flutter?

888

you need to create a controller like this: var textController = TextEditingController(); and aggregate the controller to your RichText widget, with this controller you can get the value of the text like this: textController.text

Share:
888
Pavan Boro
Author by

Pavan Boro

Updated on December 17, 2022

Comments

  • Pavan Boro
    Pavan Boro over 1 year

    Here's the Flutter code:

       Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Padding(
                      padding:
                          const EdgeInsets.only(bottom: LuminaSpacing.extraTiny),
                      child: RichText(
                        key: Key('last_updated'),
                        textAlign: TextAlign.end,
                        text: TextSpan(
                          text: '${_localize.last_updated}: ',
                          style: wldTheme.textTheme.display1,
                          children: <TextSpan>[
                            TextSpan(
                                text: '${settingStore.lastCheckinTime}',
                                style: wldTheme.textTheme.body1),
                          ],
                        ),
                      ),
                    ),
    

    I'm using appium with java to automate my test cases

    Here's my code:

    FlutterFinder ele = new FlutterFinder(driver);
    
    System.out.println("Last update: "+ele.byValueKey("last_update"));
    // System.out.println("Last update: "+ele.byValueKey("last_update").getText());  // It waits for long duration
    
    

    The output I get is

    Last update: [pro.truongsinh.appium_flutter.finder.FlutterElement@870fb657 -> unknown locator]
    
    • Hemanth Raj
      Hemanth Raj over 4 years
      Please check, you are specifying Key and expecting element by ValueKey. Try passing ValueKey<String>('last_updated') instead of Key('last_updated').
    • Pavan Boro
      Pavan Boro over 4 years
      already tried that way too but didn't work. Also tried using semantic label too.
  • gegobyte
    gegobyte about 4 years
    controller is not defined for RichText.