Skip to content

Bad state: Cannot add event while adding a stream #1759

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

Closed
MCSQNXA opened this issue Apr 29, 2025 · 10 comments
Closed

Bad state: Cannot add event while adding a stream #1759

MCSQNXA opened this issue Apr 29, 2025 · 10 comments
Labels
needs-info Additional information needed from the issue author package:http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@MCSQNXA
Copy link

MCSQNXA commented Apr 29, 2025

I tried to run the code in a web project, but encountered the error 'Bad state: Cannot add event while adding a stream'

import 'dart:async';
import 'dart:convert' as convert;

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

void main() {
runApp(const MyApp());
}

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

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
home: TextButton(onPressed: () => doS(), child: Text('data')),
);
}
}

Future doS() async {
try {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ["txt", "zip"],
withData: false,
withReadStream: true,
);

if (result == null) {
  return;
}

final req = http.StreamedRequest(
  'POST',
  Uri.parse('http://127.0.0.1/upload'),
);
req.contentLength = result.files[0].size;
req.sink.addStream(result.files[0].readStream!);

unawaited(req.sink.close());

final rsp = await req.send();

if (rsp.statusCode == 200) {
  debugPrint(convert.utf8.decode(await rsp.stream.toBytes()));
}

} catch (e) {
debugPrint(e.toString());
}
}

@MCSQNXA MCSQNXA added package:http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Apr 29, 2025
@brianquinlan
Copy link
Collaborator

I think that the problem is that you are not awaiting addStream i.e.

- req.sink.addStream(result.files[0].readStream!);
+ await req.sink.addStream(result.files[0].readStream!);

Please reopen the issue if that doesn't solve the problem.

@MCSQNXA
Copy link
Author

MCSQNXA commented May 1, 2025

Thank you for your reply, but addStream will continue to block and cannot continue running until the HTTP function is sent

@MCSQNXA
Copy link
Author

MCSQNXA commented May 1, 2025

I think that the problem is that you are not awaiting addStream i.e.

  • req.sink.addStream(result.files[0].readStream!);
  • await req.sink.addStream(result.files[0].readStream!);
    Please reopen the issue if that doesn't solve the problem.

Thank you for your reply, but addStream will continue to block and cannot continue running until the HTTP function is sent

@brianquinlan
Copy link
Collaborator

Then maybe you want to do (untested):

- req.sink.addStream(result.files[0].readStream!);
- unawaited(req.sink.close());
+ unawaited(req.sink.addStream(result.files[0].readStream!)).then((_) => req.sink.close()));

@MCSQNXA
Copy link
Author

MCSQNXA commented May 1, 2025

Then maybe you want to do (untested):

  • req.sink.addStream(result.files[0].readStream!);
  • unawaited(req.sink.close());
  • unawaited(req.sink.addStream(result.files[0].readStream!)).then((_) => req.sink.close()));

I tried, but it's still a bad state: Cannot add event while adding a stream

@brianquinlan brianquinlan reopened this May 1, 2025
@brianquinlan
Copy link
Collaborator

Could you post your exact code and the exact stacktrace that you get?

@brianquinlan brianquinlan added the needs-info Additional information needed from the issue author label May 1, 2025
@MCSQNXA
Copy link
Author

MCSQNXA commented May 1, 2025

Could you post your exact code and the exact stacktrace that you get?

import 'dart:async';
import 'dart:convert' as convert;

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

void main() {
runApp(const MyApp());
}

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

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
home: TextButton(onPressed: () => doS(), child: Text('data')),
);
}
}

Future doS() async {
try {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ["txt", "zip"],
withData: false,
withReadStream: true,
);

if (result == null) {
  return;
}

final req = http.StreamedRequest(
  'POST',
  Uri.parse('http://127.0.0.1/upload'),
);
req.contentLength = result.files[0].size;

await req.sink.addStream(result.files[0].readStream!);

unawaited(req.sink.close());

final rsp = await req.send();

if (rsp.statusCode == 200) {
  debugPrint(convert.utf8.decode(await rsp.stream.toBytes()));
}

} catch (e) {
debugPrint(e.toString());
}
}

Launching lib\main.dart on Chrome in debug mode...
Waiting for connection from debug service on Chrome...
This app is linked to the debug service: ws://127.0.0.1:49778/3NwK13chmcM=/ws
Debug service listening on ws://127.0.0.1:49778/3NwK13chmcM=/ws
Debug service listening on ws://127.0.0.1:49778/3NwK13chmcM=/ws
Bad state: Cannot add event while adding a stream

PS S:\SecludedClient\frontend> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.29.2, on Microsoft Windows [版本 10.0.26100.3915], locale zh-CN)
[✓] Windows Version (11 专业版 64-bit, 24H2, 2009)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
[✓] Chrome - develop for the web
[✓] Visual Studio - develop Windows apps (Visual Studio Enterprise 2022 17.12.5)
[!] Android Studio (not installed)
[✓] IntelliJ IDEA Ultimate Edition (version 2025.1)
[✓] VS Code (version 1.99.3)
[✓] Connected device (4 available)
[!] Network resources
✗ A network error occurred while checking "https://maven.google.com/": 信号灯超时时间已到

✗ A network error occurred while checking "https://github.com/": 远程计算机拒绝网络连接。

! Doctor found issues in 2 categories.

This is my diagnosis, and I think it should be fine because other HTTP functions in this library can run normally

@MCSQNXA MCSQNXA closed this as completed May 1, 2025
@MCSQNXA
Copy link
Author

MCSQNXA commented May 1, 2025

Could you post your exact code and the exact stacktrace that you get?

versions:

http: ^1.3.0
file_picker: ^10.1.2

@MCSQNXA MCSQNXA reopened this May 1, 2025
@brianquinlan
Copy link
Collaborator

You didn't modify your code to match my suggestion.

@MCSQNXA
Copy link
Author

MCSQNXA commented May 1, 2025

You didn't modify your code to match my suggestion.

Thank you, I have resolved it and need to modify it to

req.sink.addStream(file.readStream!).then((_) => req.sink.close());

@MCSQNXA MCSQNXA closed this as completed May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-info Additional information needed from the issue author package:http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

2 participants