Flutter에서 한글 오류깨져요

windows 환경에서 Flutter 와 dll 를 지원 해 주고 있습니다.

한글 깨짐 현상

1. 패키지 설치

pubspec.yaml 에 다음과 같이 js 패키지를 추가합니다.

dependencies:
  flutter:
    sdk: flutter
  ffi: ^2.0.1 
  charset_converter: ^2.0.1
  cupertino_icons: ^1.0.6
  intl: ^0.18.0
 
dev_dependencies:
  flutter_test:
    sdk: flutter
  ffi: ^2.0.1 
  charset_converter: ^2.0.1
  flutter_lints: ^3.0.0
  intl: ^0.18.0

추가하고 패키지를 설치합니다.

$flutter pub add charset_converter

$flutter pub get

2. dll 연결선언

import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart'; 
import 'dart:typed_data';
import 'package:charset_converter/charset_converter.dart';

// DLL 파일 로드
final DynamicLibrary PosPayLibrary = DynamicLibrary.open('PayDll_x64.dll');

typedef CEasyPay = Int32 Function(
    Pointer<Utf8> sIp, Int32 iPort, 
    Pointer<Utf8> sGd, 
    Pointer<Utf8> sSend, Int32 iSendLn, 
    Pointer<Utf8> sSignGubun,
    Pointer<Utf8> sSignPath, Int32 iSignPathLn,
    Pointer<Utf8> sRecv);

typedef DartEasyPay = int Function(
    Pointer<Utf8> sIp, int iPort, 
    Pointer<Utf8> sGd, 
    Pointer<Utf8> sSend, int iSendLn, 
    Pointer<Utf8> sSGubun,
    Pointer<Utf8> sPath, int iPathLn,
    Pointer<Utf8> sRecv);

final EasyPay = PosPayLibrary
    .lookupFunction<CEasyPay, DartEasyPay>('EasyPay');

3. main.dart 추가

//Dll 호출
String ip, gd, send, sGubun, sPath;
Pointer<Utf8> sIp = ip.toNativeUtf8();
Pointer<Utf8> sGd = gd.toNativeUtf8();
Pointer<Utf8> sSend = send.toNativeUtf8();
Pointer<Utf8> sGubun = sGubun.toNativeUtf8();
Pointer<Utf8> sPath = sPath.toNativeUtf8();
Pointer<Uint8> sRecv = calloc<Uint8>(1024);

int result = EasyPay(sIp, port, sGd, sSend, send.length, sSGubun, 
                     sPath, sPath.length, sRecv.cast<Utf8>());

4.응답데이터 한글 처리

Future<String> extractString(int length) async {
  if (offset + length > recvData.length) {
    print("Error: Buffer Overflow Detected");
    return "";
  }

  Uint8List bytes = recvData.sublist(offset, offset + length);
  offset += length;

  String result = await CharsetConverter.decode("euc-kr", bytes);
  return result.trim();
}

5. 사용

// run
flutter run -d windows
Launching lib\main.dart on Windows in debug mode...
Building Windows application...                                     8.6s
✓ Built build\windows\x64\runner\Debug\my_flutter_app.exe
Syncing files to device Windows...                                  54ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on Windows is available at: http://127.0.0.1:53106/JNRgyWRPPeI=/

정상적으로 한글 처리가 되었습니다.

참고>

jsonDecode(response.body); 🔻 jsonDecode(utf8.decode(response.bodyBytes));

Last updated