标签: flutter-http

Flutter通过rest api端点上传文件

我有一个 flutter 应用程序,它通过 REST api 与服务器交互,可以获取和显示信息。它还可以向服务器发送文本信息。我想添加将 pdf 文件和图像发送到服务器的功能,但我不知道如何实现。这是选择文件的代码

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Another Attempt'),
        centerTitle: true,
      ),
      body: Container(
        alignment: Alignment.topCenter,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            MaterialButton(
              onPressed: () async {
                FilePickerResult result =
                    await FilePicker.platform.pickFiles(allowMultiple: true);

                if (result != null) {
                  List<File> files =
                      result.paths.map((path) => File(path)).toList();
                } else {
                  // User canceled the picker
                }
              },
              child: Text('Upload multiple files'),
              color: Colors.blueAccent,
            ),
            SizedBox(height: 10.0),
            MaterialButton(
              onPressed: () async {
                FilePickerResult …
Run Code Online (Sandbox Code Playgroud)

rest dart flutter flutter-http

5
推荐指数
1
解决办法
2万
查看次数

flutter中api调用哪种方式最好

我是颤振开发的新手。最近听说了Dio和Http包用于api调用。这是 api 调用最好的一个。如果有人有更好的api服务方式?

  CreateAccountRepository.internal();

  static final CreateAccountRepository _singleInstance =
      CreateAccountRepository.internal();

  factory CreateAccountRepository() => _singleInstance;

  //api: Registration
  Future<CreateAccountResponse> userRegistration(
      AccountCreateRequest requestParams) async {
    bool isNetworkAvail = await NetworkCheck().check();
    if (isNetworkAvail) {
      final response = await networkProvider.call(
          method: Method.POST,
          pathUrl: AppUrl.pathRegister,
          body: requestParams.toJson(),
          headers: headerContentTypeAndAccept);
      if (response.statusCode == HttpStatus.ok) {
        String location = response.headers['location'];
        String userId = location.split("/").last;
        CreateAccountResponse createAccountResponse =
            CreateAccountResponse.fromJson(json.decode(response.body));
        createAccountResponse.isSuccess = true;
        createAccountResponse.userId = int.parse(userId);
        return createAccountResponse;
      } else if (response.statusCode == HttpStatus.unprocessableEntity) {
        return CreateAccountResponse.fromJson(json.decode(response.body));
      } else if …
Run Code Online (Sandbox Code Playgroud)

api rest flutter dio flutter-http

5
推荐指数
1
解决办法
7238
查看次数

当 Webview 重定向到 Flutter 中的另一个页面时获取 Json 响应

flutter_webview_plugin^0.3.11 我正在构建一个支付网关,在 3d 安全模式下我需要使用 Webview。使用 html 字符串类型的响应启动 Webview。

   return Scaffold(
  body: WebviewScaffold(
    withJavascript: true,
    key: _scaffoldKey,
    url: new Uri.dataFromString(widget.d3sc, mimeType: 'text/html').toString(),
  ),
);
Run Code Online (Sandbox Code Playgroud)

它在屏幕上打印安全代码阶段。当我提交代码时,它会自动重定向到回调 url,但我无法设法连接或从该 url 获取响应。当提交代码并且页面被重定向以获得用于关闭 3d 安全支付的令牌时,我需要得到响应。有什么想法或解决方案吗?

尝试了下面的代码。虽然它打印了重定向的 url,但响应元素中没有 json 响应。

 webView.launch(new Uri.dataFromString(widget.d3sc, mimeType: 'text/html').toString() ,withJavascript: true, withLocalStorage: true);
  webView.show();

  webView.onUrlChanged.listen((String event) async {
    print("${event} even eevent");
    setState(() {
      webView
          .evalJavascript("document.documentElement.innerText")
          .then((response) => print("$response response response "));

    });
  });
Run Code Online (Sandbox Code Playgroud)

html webview flutter flutterwebviewplugin flutter-http

4
推荐指数
1
解决办法
1万
查看次数

我如何在颤振中解析 html?

我正在使用 Flutter 并想使用 parser.dart 解析 HTML

<div class="weather-item now"><!-- now  -->
   <span class="time">Now</span>
   
    <div class="temp">19.8<span>?</span>
        <small>(23?)</small>
    </div>
   
   <table>
       <tr>
           <th><i class="icon01" aria-label="true"></i></th>
           <td>93%</td>
       </tr>
       <tr>
           <th><i class="icon02" aria-label="true"></i></th>
           <td>south 2.2km/h</td>
       </tr>
       <tr>
           <th><i class="icon03" aria-label="true"></i></th>
           <td>-</td>
       </tr>
   </table>
</div>
Run Code Online (Sandbox Code Playgroud)

使用,导入 'package:html/parser.dart';

我想得到这个数据

现在,19.8,23,93%,南 2.2km/h

我怎样才能做到这一点?

flutter flutter-web flutter-http

3
推荐指数
2
解决办法
5448
查看次数

Flutter http Package POST API 调用返回 302 而不是 200,po​​st 在 DB 中成功

嗨,当我通过Postman发出 API 发布请求时,获取正确的状态代码为 200,但使用 flutter/http 包 (v0.12.0+4) 或 DIO (v3.0.9) 包进行相同的 api 调用时,我得到的状态代码为302,但是post成功,数据保存在DB上。我怎样才能为此获得状态 200 或者有更好的方法来处理帖子中的重定向

发现了这些 git hub 问题,但没有关于如何解决这个问题的答案 https://github.com/dart-lang/http/issues/157

https://github.com/dart-lang/sdk/issues/38413

Code making API post
       ........... 

      final encoding = Encoding.getByName('utf-8');
        final headers = {
          HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
          HttpHeaders.acceptHeader: 'application/json',
        };
        //String jsonBody = jsonEncode(feedRequest.toJsonData());
        String jsonBody = feedRequest.toJsonData();
        print('response.SubmitFeedApiRequest>:' + feedRequest.toJsonData());
        print('jsonBody:>' + jsonBody);

        String url ='https://myapp';

        final response = await http.post(url,
            headers: headers, body: jsonBody, encoding: encoding);

        print('response.statusCode:' + response.statusCode.toString());

        if (response.statusCode == 200) { …
Run Code Online (Sandbox Code Playgroud)

flutter flutter-http

1
推荐指数
2
解决办法
3746
查看次数

如何在flutter中实现无限滚动?

我想无限滚动我的产品。我总共有 412 页。当我滚动到最后时,我想显示下一页的更多项目。这意味着无限滚动。这个怎么做?我已经实现了 ScrollController。

\n

这是我的 Api:\n https://www.moharaj.com.bd/api/new/collection/products?page=1

\n

这是我的模型类:

\n
// To parse this JSON data, do\n//\n//     final newCollectionProductModel = newCollectionProductModelFromJson(jsonString);\n\nimport \'dart:convert\';\n\nNewCollectionProductModel newCollectionProductModelFromJson(String str) =>\n    NewCollectionProductModel.fromJson(json.decode(str));\n\nString newCollectionProductModelToJson(NewCollectionProductModel data) =>\n    json.encode(data.toJson());\n\nclass NewCollectionProductModel {\n  NewCollectionProductModel({\n    required this.currentPage,\n    required this.data,\n    required this.firstPageUrl,\n    required this.from,\n    required this.lastPage,\n    required this.lastPageUrl,\n    required this.nextPageUrl,\n    required this.path,\n    required this.perPage,\n    required this.prevPageUrl,\n    required this.to,\n    required this.total,\n  });\n\n  final int currentPage;\n  final List<Datum> data;\n  final String firstPageUrl;\n  final int from;\n  final int lastPage;\n  final String lastPageUrl;\n  final String nextPageUrl;\n …
Run Code Online (Sandbox Code Playgroud)

dart flutter flutter-http

0
推荐指数
1
解决办法
9451
查看次数