http icon indicating copy to clipboard operation
http copied to clipboard

All runWithClient to return null

Open brianquinlan opened this issue 3 years ago • 3 comments

Fixes https://github.com/dart-lang/http/issues/769

brianquinlan avatar Aug 23 '22 16:08 brianquinlan

Can you expand a bit on the motivation? Is this specifically to write tests in package:http and be able to run run the same _test.dart on multiple platforms?

No, the motivation is to make it possible to use runWithZone where you only care about certain platforms e.g.

Client? client;
if (Platform.isIOS) {
  client = ...
}
runWithZone(MyWidget, () => client); // will get BrowserClient on web, IOClient on non-IOS VM.

brianquinlan avatar Aug 23 '22 17:08 brianquinlan

Hmm. I would write that as

if (Platform.isIOS) {
  final client = ...
  runWithZone(something, () => client);
} else {
  something();
}

Or, if you really like the pattern, you could use

Client? client;
if (Platform.isIOS) {
  client = ...
}
runWithZone(MyWidget, () => client ?? Client()); // will get BrowserClient on web, IOClient on non-IOS VM.

I don't think I see the value in a nullable signature.

natebosch avatar Aug 23 '22 17:08 natebosch

I don't think I see the value in a nullable signature.

@mit-mit Any opinion here? @natebosch is essentially proposing the existing solution. It could be that I should have just used it in the cupertino_http and cronet_http examples.

brianquinlan avatar Aug 23 '22 18:08 brianquinlan

My concern is that the else part currently isn't that easy. What would I write under something() here?

if (Platform.isIOS) {
  final client = ...
  runWithZone(something, () => client);
} else {
  something();
}

I can't just write client = IOClient(); or similar as that won't work on the web. I'd have to use config specific imports, which are hard.

But could I use something like this?

if (Platform.isIOS) {
  final client = ...
  runWithZone(something, () => client);
} else {
  run(something);
}

Or perhaps like this?

if (Platform.isIOS) {
  final client = ...
  runWithZone(something, () => client);
} else {
  runWithZone(something, http.defaultClient => client);
}

mit-mit avatar Aug 25 '22 11:08 mit-mit

Or perhaps like this?

if (Platform.isIOS) {
  final client = ...
  runWithZone(something, () => client);
} else {
  runWithZone(something, http.defaultClient => client);
}

You'd write that as:

Client? client;
if (Platform.isIOS) {
  final client = ...
}
runWithZone(MyWidget, () => client ?? Client());  // The Client() constructor will select the correct default for the platform.

This seems reasonable to me and I can document that pattern.

brianquinlan avatar Aug 25 '22 18:08 brianquinlan

OK, that sounds good. I guess when no config of the client is needed, you can also just use this?

final client = Platform.isIOS ? CupertinoClient.defaultSessionConfiguration() : Client();
runWithClient(myFunction, () => client);

mit-mit avatar Aug 26 '22 09:08 mit-mit

OK, that sounds good. I guess when no config of the client is needed, you can also just use this?

final client = Platform.isIOS ? CupertinoClient.defaultSessionConfiguration() : Client();
runWithClient(myFunction, () => client);

Yeah, that would be a good way to reuse a consistent Client instance too, which a null return couldn't achieve.

natebosch avatar Aug 26 '22 16:08 natebosch

OK, I reframed this as a documentation fix. PTAL.

brianquinlan avatar Aug 26 '22 18:08 brianquinlan