为什么 flutter 拒绝在 localhost:8000 或 127.0.01:8000 上连接?

Yos*_*oss 7 api rest flutter

我正在按照 Flutter Networking/HTTP 教程对在我的 localhost:8000 上运行的服务器执行 GET 请求。通过浏览器访问我的本地主机工作正常。当我指向任何真实的 URL 时,这也很好用,例如https://example.com,但是当我指向https://127.0.0.1:8000 时,我收到类似“连接被拒绝”的错误

每次我重新加载应用程序时,上述错误中的端口都会发生变化。我查看了 http 包代码,似乎没有办法为 URL 指定端口。我如何指向我的本地主机,这是我第一次使用 flutter 吗?PS:我在我的手机设备上运行,我的电脑和手机用同一个 wifi 连接,我的网络是私有的。

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  static const url = 'http://127.0.0.1:8000/api/membres/';

  // static const url = 'http://10.0.2.2:8000/api/membres/';
  //static const url = 'http://localhost:8000/api/membres/';
  //static const url= "192.168.1...:8000/association/api/membres";
  //static const url = 'https://jsonplaceholder.typicode.com/users';
  Future<List<Map<String, dynamic>>> _future;

  @override
  void initState() {
    super.initState();
    _future = fetch();
  }

  Future<List<Map<String, dynamic>>> fetch() {
    return http
        .get(url)
        .then((response) {
          return response.statusCode == 200
              ? response.body
              : throw 'Error when getting data';
        })
        .then((body) => json.decode(body))
        .then((list) => (list as List).cast<Map<String, dynamic>>());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: RefreshIndicator(
        onRefresh: () async {
          _future = fetch();
          setState(() {});
          return _future;
          },
        child: FutureBuilder<List<Map<String, dynamic>>>(
          future: _future,
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Center(
                child: Container(
                  constraints: BoxConstraints.expand(),
                  child: SingleChildScrollView(
                    physics: AlwaysScrollableScrollPhysics(),
                    child: Text(snapshot.error.toString()),),),);}
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );}
            return ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (BuildContext context, int index) {
                final item = snapshot.data[index];
                return ListTile(
                  title: Text(item['name']),
                  subtitle: Text(item['email']),
                );
              },
            );
          },
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Sed*_*ush 17

如果您使用的是 Android 模拟器,那么localhost在模拟器上就不是127.0.0.010.0.2.2,因此,在您需要编写的 Android 模拟器上https://10.0.2.2:8000,它https://127.0.0.1:8000也不会在真实设备上运行。因为localhost在真实设备上意味着不同的东西。

有关如何将 Flutter 应用程序连接到localhost模拟器或真实设备的更多信息,请单击链接将 Flutter 应用程序连接到本地主机


Cha*_*ghe 12

您必须保持手机和电脑相同的网络连接。

然后传递你的 url 假设你的 ip 和 url 是这样的192.168.1.102:8000

static const url= "192.168.1.102:8000/association/api/membres";
Run Code Online (Sandbox Code Playgroud)