http
http copied to clipboard
All runWithClient to return null
Fixes https://github.com/dart-lang/http/issues/769
Can you expand a bit on the motivation? Is this specifically to write tests in
package:httpand be able to run run the same_test.darton 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.
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.
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.
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);
}
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.
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);
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.
OK, I reframed this as a documentation fix. PTAL.