首页 > 解决方案 > 如何在颤动中将图像转换为字符串

问题描述

所以我有这个颤振应用程序,这个人需要从应用程序的画廊上传一些照片。如何将这些图像存储在应用程序中并将它们转换为字符串以便保存到我的数据库中?(蒙古数据库)

我想要:

  1. 将图像保存为一个值
  2. 将此值转换为字符串
  3. 将字符串图像保存到“受益人”类中

应用程序 UI 图像

class BeneficiaryServices extends StatefulWidget {
  @override
  _beneficiaryState createState() => _beneficiaryState();
}

class _beneficiaryState extends State<BeneficiaryServices>
    with View<BeneficiaryServices> {
  var data;
  bool autoValidate = true;
  bool readOnly = false;
  bool showSegmentedControl = true;
  File _image;
  final picker = ImagePicker();

  beneficiaryservicesController controller =
      new beneficiaryservicesController();
  final _formKey = GlobalKey<FormState>();
  final _fbKey = GlobalKey<FormBuilderState>();

  Future save() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);
    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    });

    var res = await http.post(Uri.parse("http://192.168.56.1:8080/signin"),
        headers: <String, String>{
          'Context-Type': 'application/json;charSet=UTF-8'
        },
        body: <String, String>{
      
        });
    print(res.body);
  }

  Beneficiary beneficiary = Beneficiary('', '', '', '');

  @override
  Widget getBody() {
    return Container(
      height: MediaQuery.of(context).size.height - 200,
      child: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 20.0,
            ),
            
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: _image == null
                  ? Text('Upload Proof of address')
                  : Image.file(_image),
            ),
            FloatingActionButton(
              onPressed: getImage,
              tooltip: 'Pick Image',
              child: Icon(Icons.add_a_photo),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: _image == null
                  ? Text('Upload Birth Certificate')
                  : Image.file(_image),
            ),
            FloatingActionButton(
              onPressed: getImage,
              tooltip: 'Pick Image',
              child: Icon(Icons.add_a_photo),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: _image == null
                  ? Text('Upload CEB/CWA/Phone Bill')
                  : Image.file(_image),
            ),
            FloatingActionButton(
              onPressed: getImage,
              tooltip: 'Pick Image',
              child: Icon(Icons.add_a_photo),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: _image == null
                  ? Text('Upload Proof of Loan (if applicable)')
                  : Image.file(_image),
            ),
            FloatingActionButton(
              onPressed: getImage,
              tooltip: 'Pick Image',
              child: Icon(Icons.add_a_photo),
            ),
            Padding(
              padding: const EdgeInsets.all(24.0),
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8.0),
                  ),
                  primary: const Color(0xffde0486),
                ),
                child: Container(
                  margin: const EdgeInsets.all(8),
                  child: const Text(
                    'Submit Application',
                    style: TextStyle(
                      color: Colors.white,
                      package: 'flutter_credit_card',
                    ),
                  ),
                ),
                onPressed: () {
                  print('valid!');
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Controller getController() {
    return this.controller;
  }

  @override
  InstanceNames getName() {
    return InstanceNames.ZakatCalculator;
  }
 void getImage() {}
}

标签: node.jsflutterdart

解决方案


final imageBytes = await pickedFile.readAsBytes();
final base64Image = base64Encode(imageBytes);

Where返回所提供字节数组base64Encode的 ba​​se64 编码表示,类型为 。String


推荐阅读