问题描述
我正在遵循Flutter Networking/HTTP教程对运行在localhost:8000上的服务器进行GET请求.通过我的浏览器访问我的本地主机工作正常.当我指向任何真实的URL(例如
您必须保持手机和计算机处于同一网络连接.
然后假设您的IP和url为 192.168.1.102:8000
静态常量url ="192.168.1.102:8000/association/api/membres";
I'm following the Flutter Networking/HTTP tutorial to do a GET request to a server running on my localhost:8000. Visiting my localhost via my browser works fine. This works fine too when I point to any real URL, such as https://example.com, but when I point to https://127.0.0.1:8000 I get an error like " connection refused "
The port in the error above changes each time I reload the app. I looked in the http package code and it doesn't seem like there is a way to specify the port for the URL. How do I point to my localhost please it's my first time with flutter ?PS: i'm running on my phone device , my pc and phone are connected with the same wifi, my network is private.
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']),
);
},
);
},
),
),
);
}
}
You have to keep your mobile phone and computer same network connection.
then pass your url assuming your ip and url is this 192.168.1.102:8000
static const url= "192.168.1.102:8000/association/api/membres";
这篇关于为什么flutter拒绝连接localhost:8000或127.0.01:8000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!