finalize method
Commits all fields and files into a stream for the final sending.
Implementation
Stream<Uint8List> finalize() {
if (isFinalized) {
throw StateError(
'The FormData has already been finalized. '
'This typically means you are using '
'the same FormData in repeated requests.',
);
}
_isFinalized = true;
final controller = StreamController<Uint8List>(sync: false);
void writeLine() => controller.add(_rnU8); // \r\n
void writeUtf8(String s) =>
controller.add(_effectiveU8Encoding(utf8.encode(s)));
for (final entry in fields) {
writeUtf8('--$boundary$_rn');
writeUtf8(_headerForField(entry.key, entry.value));
writeUtf8(entry.value);
writeLine();
}
Future<void>(() async {
for (final file in files) {
writeUtf8('--$boundary$_rn');
writeUtf8(_headerForFile(file));
await writeStreamToSink<Uint8List>(file.value.finalize(), controller);
writeLine();
}
}).then((_) {
writeUtf8('--$boundary--$_rn');
}).whenComplete(() {
controller.close();
});
return controller.stream;
}