Skip to content

Instantly share code, notes, and snippets.

@doyle-flutter
Last active February 17, 2021 09:06
Show Gist options
  • Save doyle-flutter/fb683b87d2649d8206b19b34236ab6cd to your computer and use it in GitHub Desktop.
Save doyle-flutter/fb683b87d2649d8206b19b34236ab6cd to your computer and use it in GitHub Desktop.
유튜브 및 네이버 카페 이강원님 질문(https://cafe.naver.com/flutterjames)
// webview_flutter: ^1.0.7 버전 사용
// http: ^0.12.2 버전 사용
class Req extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("웹뷰를 띄우겠습니다"),),
body: WebView(
initialUrl: 'http://127.0.0.1:3003/req',
javascriptMode: JavascriptMode.unrestricted,
javascriptChannels: {
JavascriptChannel(
onMessageReceived: (JavascriptMessage message) {
int check1 = json.decode(message.message);
print("MSG 확인 : $check1");
// 이 때 중요한 부분이 '서버는 웹화면이다' 라는 생각과 비슷하게 착각하는 것 입니다.
// '인터넷이 연결되어있다' 라는 표현이 익숙하기 때문에 브라우져와 서버의 연결이 종료된 것을
// 간혹 착각합니다(네트워크 - HTTP 관련 영상 및 도서를 보시면 좋습니다)
// HTTP는 지속적으로 연결이 필요하지 않습니다
// 사용할 때만 연결합니다 :)
// (1) 클라이언트에서 다시 서버로 요청
Future.microtask(() async{
http.Response res = await http.post(
'http://127.0.0.1:3003/req2',
body: json.encode({"id":"Flutter 에서 보낸 ID : USER_ID"}),
headers: {'Content-Type': 'application/json;charset=UTF-8'}
);
bool check2 = json.decode(res.body);
print("다시 서버에 요청한 반환 값 확인 $check2");
return;
});
return;
},
name: 'james'
)
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment