Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: PackageInfo needs HttpClient but has no parameter for it #3381

Open
8 tasks done
martinhaintz opened this issue Dec 3, 2024 · 1 comment
Open
8 tasks done
Labels
enhancement New feature or request triage

Comments

@martinhaintz
Copy link

Platform

Web

Plugin

package_info_plus

Version

8.1.1

Flutter SDK

3.24.3

Steps to reproduce

Hello,

based on this issue, I added a minimal sample code. The problem is, that the user has their own customHttpClient, which the user uses in the app. Therefore the user sets the no_default_http_client=true flag, but package_info_plus needs a httpClient, but has no parameter to set the httpClient.

Add package_info_plus: ^8.1.1 and http: ^1.2.2 to the pubspec.yml

Build with flutter build web --release --source-maps --dart-define=no_default_http_client=true

And run with python3 -m http.server 8089

Code Sample

import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:package_info_plus/package_info_plus.dart';

late final PackageInfo packageInfo;
late final Response response;

void main() async {
  response = await customHttpClient().get(Uri.parse("foo"));
  packageInfo = await PackageInfo.fromPlatform();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            children: [
              Text("packageInfo.packageName: ${packageInfo.packageName}"),
              Text("response.body: ${response.body}"),
            ],
          ),
        ),
      ),
    );
  }
}

class customHttpClient implements Client {
  @override
  void close() {}

  @override
  Future<Response> delete(Uri url,
      {Map<String, String>? headers, Object? body, Encoding? encoding}) async {
    return Response("foo", 200);
  }

  @override
  Future<Response> get(Uri url, {Map<String, String>? headers}) async {
    return Response("foo", 200);
  }

  @override
  Future<Response> head(Uri url, {Map<String, String>? headers}) async {
    return Response("foo", 200);
  }

  @override
  Future<Response> patch(Uri url,
      {Map<String, String>? headers, Object? body, Encoding? encoding}) async {
    return Response("foo", 200);
  }

  @override
  Future<Response> post(Uri url,
      {Map<String, String>? headers, Object? body, Encoding? encoding}) async {
    return Response("foo", 200);
  }

  @override
  Future<Response> put(Uri url,
      {Map<String, String>? headers, Object? body, Encoding? encoding}) async {
    return Response("foo", 200);
  }

  @override
  Future<String> read(Uri url, {Map<String, String>? headers}) async {
    return "foo";
  }

  @override
  Future<Uint8List> readBytes(Uri url, {Map<String, String>? headers}) async {
    return Uint8List(0);
  }

  @override
  Future<StreamedResponse> send(BaseRequest request) async {
    return StreamedResponse(Stream.empty(), 200);
  }
}

Logs

Uncaught Error: Bad state: no_default_http_client was defined but runWithClient was not used to configure a Client implementation.
    at Object.e (js_helper.dart:1198:19)
    at Object.ahp (browser_client.dart:23:5)
    at Object.aa1 (client.dart:42:37)
    at http.dart:165:16
    at Yj.a (async_patch.dart:303:19)
    at Yj.$2 (async_patch.dart:328:23)
    at Object.N (async_patch.dart:233:3)
    at Object.agT (http.dart:165:7)
    at Object.a0u (http.dart:165:7)
    at Object.ahL (http.dart:49:5)

Flutter Doctor

[✓] Flutter (Channel stable, 3.24.3, on macOS 15.1.1 24B91 darwin-arm64, locale en-AT)
    • Flutter version 3.24.3 on channel stable at /Users/martin/fvm/versions/3.24.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (3 months ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[!] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/martin/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

[✓] Xcode - develop for iOS and macOS (Xcode 16.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16B40
    • CocoaPods version 1.16.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)

[✓] VS Code (version 1.95.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.102.0

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 15.1.1 24B91 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 15.1.1 24B91 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 131.0.6778.86

[✓] Network resources
    • All expected network resources are available.

Checklist before submitting a bug

  • I searched issues in this repository and couldn't find such bug/problem
  • I Google'd a solution and I couldn't find it
  • I searched on StackOverflow for a solution and I couldn't find it
  • I read the README.md file of the plugin
  • I'm using the latest version of the plugin
  • All dependencies are up to date with flutter pub upgrade
  • I did a flutter clean
  • I tried running the example project
@miquelbeltran
Copy link
Member

Hello, this is not a bug but a feature request.

package_info_plus uses the http client to obtain the version.json containing the package info in web apps.

My only recommendation would be to fork the project and adapt it to your own needs, e.g. remove the web implementation or modify it to pass the http client that you prefer.

@miquelbeltran miquelbeltran added enhancement New feature or request and removed bug Something isn't working labels Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request triage
Projects
None yet
Development

No branches or pull requests

2 participants