diff --git a/example_app/openai_app/lib/bloc/openai/openai_bloc.dart b/example_app/openai_app/lib/bloc/openai/openai_bloc.dart index e71d47b..68c9203 100644 --- a/example_app/openai_app/lib/bloc/openai/openai_bloc.dart +++ b/example_app/openai_app/lib/bloc/openai/openai_bloc.dart @@ -127,17 +127,17 @@ class OpenAIBloc extends Cubit { .transform(StreamTransformer.fromHandlers(handleError: handleError)) .listen((it) { Message? message; - for (final m in list) { - if (m.id == '${it.id}') { - message = m; - list.remove(m); - break; + list.removeWhere((element){ + if(element.id == '${it.id}'){ + message = element; + return true; } - } + return false; + }); ///+= message message?.message = - '${message.message ?? ""}${it.choices.last.message?.content ?? ""}'; + '${message?.message ?? ""}${it.choices.last.message?.content ?? ""}'; list.add(Message(isBot: true, id: '${it.id}', message: message?.message)); emit(ChatCompletionState( isBot: true, messages: list, showStopButton: true)); diff --git a/lib/src/model/complete_text/response/usage.dart b/lib/src/model/complete_text/response/usage.dart index 4d8f75d..2829a64 100644 --- a/lib/src/model/complete_text/response/usage.dart +++ b/lib/src/model/complete_text/response/usage.dart @@ -1,17 +1,15 @@ class Usage { - final int promptTokens; - final int completionTokens; - final int totalTokens; + final int? promptTokens; + final int? completionTokens; + final int? totalTokens; final String id = "${DateTime.now().millisecondsSinceEpoch}"; Usage(this.promptTokens, this.completionTokens, this.totalTokens); factory Usage.fromJson(Map json) => Usage( - json['prompt_tokens'] as int, - json['completion_tokens'] == null - ? 0 - : json['completion_tokens'] as int, - json['total_tokens'] as int, + json['prompt_tokens'], + json['completion_tokens'], + json['total_tokens'], ); Map toJson() => usageToJson(this);