<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mouli150388</title>
    <description>The latest articles on DEV Community by mouli150388 (@mouli150388).</description>
    <link>https://dev.to/mouli150388</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F923646%2Fbe139315-f577-462e-a9fd-5de6c8d42849.png</url>
      <title>DEV Community: mouli150388</title>
      <link>https://dev.to/mouli150388</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mouli150388"/>
    <language>en</language>
    <item>
      <title>Flutter Rest API Integration with Login and Registration pages</title>
      <dc:creator>mouli150388</dc:creator>
      <pubDate>Sun, 11 Sep 2022 03:45:33 +0000</pubDate>
      <link>https://dev.to/mouli150388/flutter-rest-api-integration-with-login-and-registration-pages-2626</link>
      <guid>https://dev.to/mouli150388/flutter-rest-api-integration-with-login-and-registration-pages-2626</guid>
      <description>&lt;p&gt;Flutter is hybrid open source framework introduced by google. With flutter we can develope crosplatform applications like Android,ios, Linux, macos,Windows with sinlge codebase.&lt;/p&gt;

&lt;p&gt;In this example we are going to cover rest API integration in Flutter. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Building the Flutter app&lt;/li&gt;
&lt;li&gt;Interfacing with the REST api&lt;/li&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Create User Account&lt;/li&gt;
&lt;li&gt;Building the UI&lt;/li&gt;
&lt;li&gt;Login screen&lt;/li&gt;
&lt;li&gt;Signup Screen&lt;/li&gt;
&lt;li&gt;Home Screen&lt;/li&gt;
&lt;li&gt;Complete Code of the Rest API Integration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This example contains the below backend PHP files&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mysqli_connect.php&lt;/li&gt;
&lt;li&gt;login.php&lt;/li&gt;
&lt;li&gt;registration.php&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Front end having the below page&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;signin.dart&lt;/li&gt;
&lt;li&gt;signup.dart&lt;/li&gt;
&lt;li&gt;home.dart&lt;/li&gt;
&lt;li&gt;main.dart
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkw9vvenshd1w40jbbei.png" alt="Flutter Rest API Integration with Login and Registartion pages" width="800" height="607"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Service Layer This flutter login example will communicate with backend system, so in our application folder create a file api.dart and add server endpoints (API URLS)&lt;/p&gt;

&lt;p&gt;We have created a file api.dart file and added all API information&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const String ROOT="http://tutionteacher.rrtutors.com";&lt;br&gt;
const String REGISTRATION="$ROOT/api/registration.php";&lt;br&gt;
const String LOGIN="$ROOT/api/login.php";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Flutter User Authentication&lt;/p&gt;

&lt;p&gt;Here is the code for authentication api integration in flutter&lt;/p&gt;

&lt;p&gt;`login(email,password) async&lt;br&gt;
  {&lt;br&gt;
   Map data = {&lt;br&gt;
      'email': email,&lt;br&gt;
      'password': password&lt;br&gt;
    };&lt;br&gt;
    print(data.toString());&lt;br&gt;
    final  response= await http.post(&lt;br&gt;
        Uri.parse(LOGIN),&lt;br&gt;
        headers: {&lt;br&gt;
          "Accept": "application/json",&lt;br&gt;
          "Content-Type": "application/x-www-form-urlencoded"&lt;br&gt;
        },&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    body: data,
    encoding: Encoding.getByName("utf-8")
)  ;
setState(() {
  isLoading=false;
});
if (response.statusCode == 200) {
  Map&amp;lt;String,dynamic&amp;gt;resposne=jsonDecode(response.body);
  if(!resposne['error'])
  {
    Map&amp;lt;String,dynamic&amp;gt;user=resposne['data'];
    print(" User name ${user['id']}");
    savePref(1,user['name'],user['email'],user['id']);
    Navigator.pushReplacementNamed(context, "/home");
  }else{
    print(" ${resposne['message']}");
  }
  scaffoldMessenger.showSnackBar(SnackBar(content:Text("${resposne['message']}")));

} else {
  scaffoldMessenger.showSnackBar(SnackBar(content:Text("Please try again!")));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
  savePref(int value, String name, String email, int id) async {&lt;br&gt;
    SharedPreferences preferences = await SharedPreferences.getInstance();&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  preferences.setInt("value", value);
  preferences.setString("name", name);
  preferences.setString("email", email);
  preferences.setString("id", id.toString());
  preferences.commit();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
}`&lt;/p&gt;

&lt;p&gt;In the above method email and password are the request params, these params we are passing the the http post method by add inside map to body attribute&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Map data = { 'email': email, 'password': password };&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After calling the login api, we are check the response, if the response code received as 200 response.statusCode == 200 then we need to JSON Parse to check the user details&lt;/p&gt;

&lt;p&gt;In this we are doing the JSON Parse by jsonDecode method&lt;/p&gt;

&lt;p&gt;`Mapresponse=jsonDecode(response.body);&lt;/p&gt;

&lt;p&gt;if(!response['error']) {&lt;/p&gt;

&lt;p&gt;Mapuser=response['data'];&lt;/p&gt;

&lt;p&gt;print(" User name ${user['id']}");&lt;/p&gt;

&lt;p&gt;savePref(1,user['name'],user['email'],user['id']);&lt;/p&gt;

&lt;p&gt;Navigator.pushReplacementNamed(context, "/home");&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;else{&lt;/p&gt;

&lt;p&gt;print(" ${resposne['message']}");&lt;/p&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User Session management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After login api success response we are saving the user session in Shared Preference. This data we can use it when we start the application. If the session data available we will navigate user directly homescreen&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Save User Session:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;`savePref(int value, String name, String email, int id) async {&lt;/p&gt;

&lt;p&gt;SharedPreferences preferences = await SharedPreferences.getInstance();&lt;/p&gt;

&lt;p&gt;preferences.setInt("value", value);&lt;/p&gt;

&lt;p&gt;preferences.setString("name", name);&lt;/p&gt;

&lt;p&gt;preferences.setString("email", email);&lt;/p&gt;

&lt;p&gt;preferences.setString("id", id.toString());&lt;/p&gt;

&lt;p&gt;preferences.commit();&lt;/p&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Validate user session:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;`var _loginStatus=0; getPref() async {&lt;/p&gt;

&lt;p&gt;SharedPreferences preferences = await SharedPreferences.getInstance();&lt;/p&gt;

&lt;p&gt;setState(() {&lt;/p&gt;

&lt;p&gt;_loginStatus = preferences.getInt("value")!;&lt;/p&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;Download complete source code for &lt;a href="https://rrtutors.com/tutorials/flutter-user-registration-and-login-with-php-mysql" rel="noopener noreferrer"&gt;Flutter Rest API Integration with login and registarion pages&lt;/a&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>programming</category>
      <category>beginners</category>
      <category>android</category>
    </item>
  </channel>
</rss>
