Aia's Blog
Aia's Blog

Reputation: 121

how to create conditional statement with string in Flutter?

I am trying to create an app with a flutter on android studio 3.5.2, everything is fully updated,

the concept is this, There is a text field, and a Submit button, and within the submit button, three strings/text needs to be valid to get inside,

if somebody typed a word that won't match it will show, no words exist,

for example,

Strings - Philippines, Dubai, Japan

 if { 
       textfield does not match string,
       Show text hint - Does not match data,
    }
 Else
    {
       Welcome from "string"
    }

I'm still new, and I know this is not the exact code, but I hope somebody can help me translate this for flutter code, thank you to whoever will help.

Upvotes: 2

Views: 10611

Answers (3)

Ravinder Kumar
Ravinder Kumar

Reputation: 8010

Declare a list of string you need to match,

List<String> mList = ['philippines', 'dubai', 'japan'];

then match your textfiled string like this,

    var myStr = 'dubai';//or your textFieldController.text
    if (mList.contains(myStr)) {
      print('Welcome from ${myStr}');
    } else {
      print('Does not match data');
    }

Or as @Rahul Patel Suggested in his answer you do it by ternary operator,

mList.contains(myStr) ? print('Welcome from ${myStr}') : print('Does not match data');

Upvotes: -1

Kavin-K
Kavin-K

Reputation: 2117

Try this,

import 'package:flutter/material.dart';

class Question1 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _Question1State();
  }
}

class _Question1State extends State<Question1> {
  TextEditingController _textController = TextEditingController();

  String infoText = '';

  List<String> countryList = [
    'philippines',
    'dubai',
    'japan',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Padding(
        padding: EdgeInsets.all(20.0),
        child: Column(
          children: <Widget>[
            TextField(
              controller: _textController,
              decoration: InputDecoration(
                hintText: 'Enter Country',
              ),
            ),
            SizedBox(
              height: 25.0,
            ),
            FlatButton(
              onPressed: () {
                _validateUserInput(_textController.text);
              },
              child: Text('Submit'),
              color: Colors.blue,
            ),
            SizedBox(
              height: 25.0,
            ),
            Text(
              infoText,
              style: TextStyle(color: Colors.red),
            ),
          ],
        ),
      ),
    );
  }

  _validateUserInput(String input) {
    if (!countryList.contains(input.toLowerCase())) {
      setState(() {
        infoText = 'Does not match data';
      });
    } else {
      setState(() {
        infoText = 'Welcome from $input';
      });
    }
  }
}

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 53

You can use a ternary operator to solve this issue, it is a simple condensed syntax for a conditional if else. The syntax is as follows:

(some conditional || or other conditional && required conditional)?someMethodToHandleSuccess:someMethodToHandleFailure

The (conditional)?ifTrue:ifFalse is the general syntax. Hope this helps!

Upvotes: -2

Related Questions