英文:
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
以下是翻译好的部分:
- 
通过以下几个简单的步骤可以轻松完成此操作:
 - 
在nodejs中创建API(您已经有了)
 - 
将您的计算机连接到WiFi网络,然后在终端上键入
ipconfig命令。复制IPv4地址,然后您可以以以下方式从移动应用程序(flutter)向您的节点服务器发出请求: - 
如果您使用的是物理模拟器(您的手机),请确保它连接到与您的计算机相同的网络。
 - 
发送请求:
 
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'}),
        );
- 访问响应体:
 
variableName = jsonDecode(response.body);
- 发送GET请求:
 
http.Response response = await http.get(
          Uri.parse('IPAddress/YOUR-API'),
          headers: {
            'Content-Type': 'application/json; charset=UTF-8',
          },
        );
        print(response.body);
- 
您可以将上述代码包装在一个函数中,并将函数放在有状态的小部件中。
 - 
稍后,您可以从
void initState()中调用此函数。 - 
我在我的应用程序中总是使用这种实现方式。希望它对您也有效。
 
英文:
You can simply do this by following few simple steps.
- Creating the API in nodejs (which you already have)
 - Connect your computer to a wifi network and type command 
ipconfigon 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: - 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('http://192.168.68.58:4000/YOUR-API'),
          headers: {
            'Content-Type': 'application/json; charset=UTF-8',
          },
          body: jsonEncode(
              <String, String>{'username': "Amarendra", 'password': "baahubali"}),
        );
Access the reponse body:
  variableName = jsonDecode(response.body);
Get Request:
  http.Response response = await http.get(
      Uri.parse('IPAddress/YOUR-API'),
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
      },
    );
    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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论