【发布时间】:2021-01-24 21:15:01
【问题描述】:
当我按下按钮时,我只想将一些文本从颤振发送到我的节点 js 应用程序。
我想使用 http.dart 将“text2”发送到节点 js 应用程序
当我按下凸起的按钮时,我希望它将带有变量文本 2 的 http 字符串发送到节点 js 应用程序。
我想获得一个教程链接,或者只是一些帮助,因为我在网上找不到任何东西,谢谢。
我的颤振代码:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(new MyApp());
}
var text2;
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Bot Discord App',
theme: new ThemeData(
primarySwatch: Colors.blue,
primaryColor: const Color(0xFF2196f3),
accentColor: const Color(0xFF2196f3),
canvasColor: const Color(0xFFfafafa),
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final myController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Bot Discord App'),
),
body:
new Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new SizedBox(
width: 100.0,
height: 100.0,
child:
new Padding(
child:
new Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Text(
"Precense",
style: new TextStyle(fontSize:19.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
),
new TextField(
style: new TextStyle(fontSize:12.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
)
]
),
padding: const EdgeInsets.all(10.0),
),
),
new SizedBox(
width: 100.0,
height: 50.0,
child:
new Padding(
child:
new RaisedButton(key:null,
onPressed: () {
return
text2 = Text(myController.text) as String;
},
color: const Color(0xFFe0e0e0),
child:
new Text(
"UPDATE",
style: new TextStyle(fontSize:23.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
)
),
padding: const EdgeInsets.fromLTRB(6.0, 6.0, 7.0, 6.0),
),
)
]
),
);
}
void buttonPressed(){}
}
【问题讨论】:
-
如果你想做一个聊天应用,你也可以看看socket.io socket.io和dart包:pub.dev/packages/flutter_socket_io。网上有很多socketio教程。
-
我只想要一个简单的代码,将一个字符串发送到一个 IP 就可以了
-
这里是一个flutter Http Post的例子。您可以通过这种方式发送数据:flutter.dev/docs/cookbook/networking/send-data。在您的 nodejs 服务器中创建一个 post 路由并使用此代码发出请求。