如何将Node JS API与Flutter APP集成?

huangapple go评论80阅读模式
英文:

How to integrate Node JS API with Flutter APP?

问题

我已经创建了一个简单的API来存储用户名和密码在Node JS中。现在我想要与Flutter连接。
而且我在Flutter中使用http包HTTP版本
所以有人可以帮助我吗?

我已经尝试过这个了...

void _saveForm() {
    if (_formKey.currentState!.validate()) {
      _formKey.currentState!.save();
    }

    var data = {
      "email": formData['email'].toString(),
      "password": formData['password'].toString(),
    };
    // ignore: avoid_print
    print(data);

    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => Home(),
      ),
    );
  }
英文:

I have created simple API to store Username and Password in Node JS. Now i want to connect with Flutter.
And I am using http package in flutter.HTTP version
So anyone can help me with this ?

I have tried this..

void _saveForm() {
    if (_formKey.currentState!.validate()) {
      _formKey.currentState!.save();
    }

    var data = {
      "email": formData['email'].toString(),
      "password": formData['password'].toString(),
    };
    // ignore: avoid_print
    print(data);

    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => Home(),
      ),
    );
  }

答案1

得分: 0

以下是翻译好的部分:

  1. 通过以下几个简单的步骤可以轻松完成此操作:

  2. 在nodejs中创建API(您已经有了)

  3. 将您的计算机连接到WiFi网络,然后在终端上键入ipconfig命令。复制IPv4地址,然后您可以以以下方式从移动应用程序(flutter)向您的节点服务器发出请求:

  4. 如果您使用的是物理模拟器(您的手机),请确保它连接到与您的计算机相同的网络。

  5. 发送请求:

http.Response response = await http.post(
          Uri.parse('http://192.168.68.58:4000/YOUR-API'),
          headers: {
            'Content-Type': 'application/json; charset=UTF-8',
          },
          body: jsonEncode(
              <String, String>{'username': 'Amarendra', 'password': 'baahubali'}),
        );
  1. 访问响应体:
variableName = jsonDecode(response.body);
  1. 发送GET请求:
http.Response response = await http.get(
          Uri.parse('IPAddress/YOUR-API'),
          headers: {
            'Content-Type': 'application/json; charset=UTF-8',
          },
        );
        print(response.body);
  1. 您可以将上述代码包装在一个函数中,并将函数放在有状态的小部件中。

  2. 稍后,您可以从void initState()中调用此函数。

  3. 我在我的应用程序中总是使用这种实现方式。希望它对您也有效。

英文:

You can simply do this by following few simple steps.

  1. Creating the API in nodejs (which you already have)
  2. Connect your computer to a wifi network and type command ipconfig on the terminal. Copy the ipv4 address and then you can make a request to your node server from the mobile app (flutter) in the following way:
  3. If your using the physical emulator (your mobile phone) make sure it is connected to the same network as your pc is.

Post Request:

http.Response response = await http.post(
          Uri.parse(&#39;http://192.168.68.58:4000/YOUR-API&#39;),
          headers: {
            &#39;Content-Type&#39;: &#39;application/json; charset=UTF-8&#39;,
          },
          body: jsonEncode(
              &lt;String, String&gt;{&#39;username&#39;: &quot;Amarendra&quot;, &#39;password&#39;: &quot;baahubali&quot;}),
        );

Access the reponse body:

  variableName = jsonDecode(response.body);

Get Request:

  http.Response response = await http.get(
      Uri.parse(&#39;IPAddress/YOUR-API&#39;),
      headers: {
        &#39;Content-Type&#39;: &#39;application/json; charset=UTF-8&#39;,
      },
    );
    print(response.body);

You can wrap the above code in a function, and place the function in the stateful widget.

Later you can call this function from the void initState()

I always make this implementation in my apps. Hope it works for you as well.

huangapple
  • 本文由 发表于 2023年1月6日 17:32:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75029148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定