1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to solve the "type 'null' is not a subtype of type object" error

Discussão em 'Mobile' iniciado por Stack, Outubro 17, 2024 às 05:22.

  1. Stack

    Stack Membro Participativo

    I want to make a set profile page that when user must upload the profile picture, and if the upload image success it will make the auth success and redirect to homepage.But, when I run the code below, it shows the error that says "type 'null' is not a subtype of type object"

    this is my model class for the Sign Up Form

    class SignUpFormModel {
    final String? name;
    final String? email;
    final String? password;
    final String? profilePicture;

    SignUpFormModel({
    this.name,
    this.email,
    this.password,
    this.profilePicture,
    });

    Map<String, dynamic> toJson() {
    return {
    'name': name,
    'email': email,
    'username': password,
    'profile_picture': profilePicture,
    };
    }

    SignUpFormModel copyWith({
    String? profilePicture,
    }) =>
    SignUpFormModel(
    name: name,
    email: email,
    password: password,
    profilePicture: profilePicture ?? this.profilePicture,
    );
    }



    And this is my set profile page

    import 'dart:convert';
    import 'dart:io';

    import 'package:another_flushbar/flushbar.dart';
    import 'package:baca_app/blocs/auth/auth_bloc.dart';
    import 'package:baca_app/models/sign_up_form_model.dart';
    import 'package:baca_app/shared/shared_methods.dart';
    import 'package:baca_app/shared/theme.dart';
    import 'package:baca_app/ui/pages/sign_up_success_page.dart';
    import 'package:baca_app/ui/widgets/buttons.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_bloc/flutter_bloc.dart';
    import 'package:image_picker/image_picker.dart';

    class SignUpSetProfilePage extends StatefulWidget {
    final SignUpFormModel data;

    const SignUpSetProfilePage({
    super.key,
    required this.data,
    });

    @override
    State<SignUpSetProfilePage> createState() => _SignUpSetProfilePageState();
    }

    class _SignUpSetProfilePageState extends State<SignUpSetProfilePage> {
    XFile? selectedImage;

    selectImage() async {
    final imagePicker = ImagePicker();
    final XFile? image =
    await imagePicker.pickImage(source: ImageSource.gallery);

    if (image != null) {
    setState(() {
    selectedImage = image;
    });
    }
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    body: BlocConsumer<AuthBloc, AuthState>(
    listener: (context, state) {
    if (state is AuthFailed) {
    showCustomSnackbar(context, state.e);
    }
    if (state is AuthSuccess) {
    Navigator.pushNamedAndRemoveUntil(
    context, '/home', (route) => false);
    }
    },
    builder: (context, state) {
    if (state is AuthLoading) {
    return const Center(
    child: CircularProgressIndicator(),
    );
    }
    return ListView(
    padding: const EdgeInsets.symmetric(
    horizontal: 24,
    ),
    children: [
    Container(
    padding: const EdgeInsets.all(22),
    decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(20),
    color: whiteColor,
    ),
    child: Column(
    children: [
    const SizedBox(
    height: 50,
    ),
    Text(
    'Daftar',
    style: opensansGreyTextStyle.copyWith(
    fontSize: 24,
    fontWeight: bold,
    ),
    ),
    const SizedBox(
    height: 8,
    ),
    Text(
    'Masukin foto profil yuk',
    style: robotoSoftGreyTextStyle.copyWith(
    fontSize: 16,
    fontWeight: regular,
    ),
    ),
    const SizedBox(
    height: 24,
    ),
    GestureDetector(
    onTap: () {
    selectImage();
    },
    child: Container(
    width: 120,
    height: 120,
    decoration: BoxDecoration(
    shape: BoxShape.circle,
    color: lightBackgroundColor,
    image: selectedImage == null
    ? null
    : DecorationImage(
    fit: BoxFit.cover,
    image: FileImage(
    File(
    selectedImage!.path,
    ),
    ),
    ),
    ),
    child: selectedImage != null
    ? null
    : Center(
    child: Image.asset(
    'assets/ic_upload.png',
    width: 32,
    ),
    ),
    ),
    ),
    const SizedBox(
    height: 16,
    ),
    Text(
    'Shayna Hanna',
    style: opensansGreyTextStyle.copyWith(
    fontSize: 18,
    fontWeight: medium,
    ),
    ),
    const SizedBox(
    height: 30,
    ),
    CustomFilledButton(
    title: 'Continue',
    onPressed: () {
    if (selectedImage == null) {
    ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
    content: const Text(
    'Gambar tidak boleh kosong',
    ),
    backgroundColor: redColor,
    ),
    );
    } else {
    context.read<AuthBloc>().add(
    AuthRegister(
    widget.data.copyWith(
    profilePicture:
    'data:image/png;base64,${base64Encode(File(selectedImage!.path).readAsBytesSync())}',
    ),
    ),
    );
    }
    }),
    ],
    ),
    ),
    const SizedBox(
    height: 50,
    ),
    ],
    );
    },
    ),
    );
    }
    }



    I make my profilePicture fields optional but i don't understand why this error show up

    Continue reading...

Compartilhe esta Página