feat:add dio code gen

This commit is contained in:
laiiihz
2023-12-20 17:31:42 +08:00
parent 1af24fad17
commit 73f415c667
6 changed files with 243 additions and 86 deletions

View File

@ -0,0 +1,26 @@
import 'package:code_builder/code_builder.dart';
Block declareTryCatch({
required List<Expression> body,
required Map<String?, List<Expression>> onError,
bool showStackStrace = false,
}) {
return Block((b) {
b.statements.add(const Code('try'));
b.statements.add(const Code('{'));
b.statements.addAll(body.map((e) => e.statement).toList());
final entries = onError.entries;
for (var error in entries) {
b.statements.add(const Code('}'));
if (error.key != null) {
b.statements.add(Code('on ${error.key}'));
}
b.statements.add(Code(showStackStrace ? 'catch(e,s)' : 'catch(e)'));
b.statements.add(const Code('{'));
b.statements.addAll(error.value.map((e) => e.statement).toList());
if (entries.last.key == error.key) b.statements.add(const Code('}'));
}
});
}