How to integrate flutter windows app with firebase?

514

-- First, use the firedart plugin on dependencies. import the http package file into your file and update your pubspec.yaml file.

dependencies:
  http:

-- Secondly, use HTTP request for flutter desktop. We have added as http to avoid naming clashes.

import 'package:http/http.dart' as http;

-- Generating request To generate the post request we need the URL of our database which is available at the top of our database screen.

http.post("https://fir-flutted60b0.firebaseio.com/userprofile.json",
        body: json.encode());
                             or
url =" https://fir-flutted60b0.firebaseio.com/userprofile.json";
http.post(url,body:json.encode());

Here we have also added “/userprofile.json” at the end of the URL. This is very important to add because the name “userprofile” is used as the field which is used to store the various attributes with a unique Id auto-generated by the firebase.

encode() statement is used to convert the data into JSON format.

-- Code Implementation:

sendData() {
    http.post("https://fir-flutter-d60b0.firebaseio.com/userprofile.json",
        body: json.encode({
          'firstName': firstNameController.text,
          'lastName': lastNameController.text,
          'email': emailController.text,
        }));
    setState(() {
      userProfile.add(Profile(
        firstName: firstNameController.text,
        lastName: lastNameController.text,
        email: emailController.text,
      ));
    });
  }

-- Sending GET request To fetch the data we will again need the same URL.

final response = await http.get("https://fir-flutter d60b0.firebaseio.com/userprofile.json?"); Here we have taken an empty list named loadedProfile which is a list of widgets that will be updated every time the user adds a new profile. final List loadedProfile = [];

We will the JSON data from the database, therefore, we will need to add a decode() statement. NOTE: To use encode() and decode() statements we need to import

import 'dart:convert';

Here extractedData is a variable that will store the profile. Each element of the response needs to be converted into a widget to provide a responsive UI for the user, therefore we have used the forEach() and add() statement to identify every new profile and add Profile widget into the loadedProfile list final extractedData = json.decode(response.bo

Preparing UI NewUser Widget In this widget, we have three TextField() and a FlatButton() which will take add from the user and generate a get request when the FlatButton() is pressed. Now we need three different controllers for all three

TextField().
final firstNameController = TextEditingController();
final lastNameController = TextEditingController();
final emailController = TextEditingController();
Share:
514
Omar Abdelazeem
Author by

Omar Abdelazeem

Updated on January 01, 2023

Comments

  • Omar Abdelazeem
    Omar Abdelazeem over 1 year

    I am trying to create windows app with flutter and I want to use firebase for authentication and firestore for storing data , So how can I integerate it ?