【问题标题】:Why does htttp.get give me an Error in Flutter为什么 http.get 在 Flutter 中给我一个错误
【发布时间】:2021-06-28 06:06:51
【问题描述】:

我试图从 Python 脚本中获取值,如下所示:

    from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/', methods = ['GET'])
def index():
    return jsonify({"greetings" : "Hi this is Python"})


if __name__ == '__main__':
    app.run(debug = True)

在 launichg 得到以下输出后:

    C:\Users\finn1\PycharmProjects\FLASK_FLUTTER_APP\venv\Scripts\python.exe C:/Users/finn1/PycharmProjects/FLASK_FLUTTER_APP/main.py
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: ***-***-***
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

我像这样在 Flutter 中导入了 http 库:

import 'package:http/http.dart';

然后我尝试使用“http.get”获取数据:

child: IconButton(
                icon: new Icon(
                  Icons.settings,
                ),
                onPressed: () async {

                  final response = await http.get("http://127.0.0.1:5000/");

但我在启动我的应用程序时收到以下错误:

    Error: The getter 'http' isn't defined for the class '_HomepageState'.
 - '_HomepageState' is from 'package:what_todo/screens/homepage.dart' ('lib/screens/homepage.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'http'.
                      final response = await http.get("http://127.0.0.1:5000/");

感谢您的帮助!

【问题讨论】:

    标签: python flutter http http-get


    【解决方案1】:

    尝试使用import 'package:http/http.dart' as http;

    package example

    【讨论】:

    • 更多解释,您尝试从http package 调用get,但您尝试使用http.get。所以你需要指定一个导入前缀才能从导入中寻找函数,比如import *** as http;
    猜你喜欢
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2017-12-17
    • 2013-11-14
    • 1970-01-01
    相关资源
    最近更新 更多